Packaging Python Projects & PIP reqs

Whenever we want to setup a specific project we need to make sure the setup is structured such that it is intuitive and it feed in to other directories properly. At the moment I am starting a brand new project with friends and it is important to make sure that when setting it up, the project adheres to rules and structures that make it easy to reproduce and export.

The first thing you will have in your initial package of functions, models, and classes is an __init__.py, which is used to import the directory as a package and you don’t mess with it. Along side that would be the py file that you are filling with your code.

Inside the main directory you will also want to have a README.md that will help those that wish to better understand the process and functionality of the project. A License write up (if needed), setup.cfg (for setting up), and a folder for tests should also all be in the main.

Now for the setup you can either do it as a .py file or a .cfg file. The cfg file is “static”, meaning that it is guaranteed to be the same every time. It is the simpler of the two and as such is less error prone. The .py file on the other hand is “dynamic”, so it is possibly non-deterministic. So anything that is dynamic or initialized at install time are meant to be in the setup.py file.

Creating the README.md is next and it is important to keep in mind as it will often be projected in your github. Similarly the License is there to make sure that the permissions and rights to the property are secured correctly.

One thing that wasn’t mentioned was the requirements.txt file. It is rather easy to setup as you do

$ pip install -r requirements.txt

It is important that it be placed in a directory where it will be executed. Otherwise you need to make sure the path is specified. The requirements.txt file is a space where all of the python libraries can be managed with pip and to source similar means. You need to set it up such that they are housed with the versions.

Finally if you need to output the package you need to do

$ pip freeze

and that will allow others access to this package that you setup.

resources:

https://packaging.python.org/tutorials/packaging-projects/

https://note.nkmk.me/en/python-pip-install-requirements/