New Project Setup
This guideline shows how to set up a standard Python project. It features, how to structure the project and installing helping packages.
Used packages [1]
Name |
Description |
URL |
|---|---|---|
Cookiecutter |
Create project file structure |
|
pip-tools |
Manage package dependencies |
|
MyPy |
Static type checker |
|
Flake8 |
Linter |
|
Pytest |
Unit Testing |
|
coverage.py |
Code Coverage Measurement |
|
tox |
Run tests in various Python environments |
|
Travis CI |
Continuous Integration Server |
|
Sphinx |
Documentation |
|
pyenv |
Virtual Environment |
* optional
Create project structure
Cookiecutter creates a project structure based on predefined templates. This saves you the effort of manually adding various files and folder structures.
Because you need cookiecutter only once, at the beginning of your project, it is recommended to install it outside of your project’s virtual environment.
Create project
Now we’re ready to create the project based on our slightly adapted template:
$ cd /my/project/root/dir
$ cookiecutter /path/to/cookiecutter/template/root/dir
For example:
$ mkdir ~/best_practice_project
$ cd ~/best_practice_project
$ cookiecutter ~/my_templates/cookiecutter-pypackage
Define your project parameters in the wizard.
Create your project’s virtual environment
We recommended to create a virtual environment using pyenv <install_pyenv>
(with the pyenv-virtualenv extensions).
Create virtual environment via:
$ pyenv virtualenv <PYTHON-VERSION> <VENV_NAME>
Alternatively, use may use Python’s built-in venv via
$ python -m venv <VENV_NAME>
It uses the venv module and creates a virtual environment named <VENV_NAME> inside your current directory (you may also specify an absolute path).
As a second alternative, the module virtualenv can also be used. Although, it has to be installed into the Python environment (from which to create the virtual environment from) first:
$ pip install virtualenv
$ virtualenv <VENV_NAME>
Same as for Python’s built in venv module, it creates a virtual environment in your current directory.
A major difference of virtualenv is that its created environment is autonomous of its originating interpreter. The virtual environment from venv and pyenv require resources from the original interpreter (Pro: they are smaller).
Install dependencies
First, install pip-tools, which we use to manage our dependencies (activate your project’s virtual environment first):
(project_venv) $ pip install pip-tools
Important
pip-tools must be installed into the project’s virtual environment. You must
not use a pip-tools installation from a different environment (e.g. from a
pipx installation), because pip-sync installs into the environment it
is called from.
Run pip-compile --version and pip-sync --version to verify the installation success.
All requirements for development as mentioned in the overview table
are already listed in requirements-dev.in. To create a requirements-dev.txt,
execute:
(project_venv) $ pip-compile requirements-dev.in -o requirements-dev.txt
This produces the *.txt file containing our dependencies plus all required packages for those dependencies (all using pinned versions). To install all these dependencies, execute:
(project_venv) $ pip-sync requirements-dev.txt
Hint
All dependencies which are required to execute our project are gathered
in requirements.in. Same workflow here:
(project_venv) $ pip-compile requirements.in -o requirements.txt
(project_venv) $ pip-sync requirements.txt