As I currently am putting some Python code in production, I though that sharing some tricks and pakcages might be a good idea.
Project Structure
Let’s first start with an overview of what your project’s structure should look like for the most basic use case of publishing it to GitHub for other developers :
- README.md
- LICENSE.txt
- requirements.txt
- app
- files.py
- tests.py
The file files.py
is your project. The file test.py
implements unit tests. The file requirements.txt
specifies any depenencies needed in your project, with the exact version.
Automatically collect your requirements
A famous option to save your requirements in a file automatically is to use pip freeze
. However :
- pip freeze only saves the packages that are installed with pip install in your environment.
- pip freeze saves all packages in the environment including those that you don’t use in your current project. (if you don’t have virtualenv)
- and sometimes you just need to create requirements.txt for a new project without installing modules.
For this reason, using pipreqs
is better. The GitHub project can be found here. After installing the package, simply run :
pipreqs /home/project/location
And a requirements.txt
file containing all dependencies and versions will be created. For example :
spacy==2.0.18
pandas==0.23.0
numpy==1.15.4
AutoPEP8
PEP8 is a standard for Python programming. It specifies the number of spaces in each case, the number of lines between 2 sections and much more. There are tools to automatically “clean” your code so that they fit with the PEP8 format. AutoPep8 is the most famous option to do this. Simply run :
autopep8 --in-place --aggressive --aggressive <filename>
And your code will be modified accoring to PEP8 standards. AutoPep8 will fix indentation issues, lines that are too long, lists that are too long…