In this post, I introduce how to start Python project.
pyenv
Install and setup pyenv by following the instruction below,
On macOS, using homebrew is easy, but I use git clone
.
After installation and setup, install some versions of Python. In this post, I use Python 3.9.1.
pyenv install 3.9.1
Pipenv
pipenv is a software for package management and virtual environment.
Installation
Install pipenv by the following instruction below,
After installation, add the code below to the setting file of shell (e.g. .bashrc
or .zshrc
) to create .venv/
under the project root.
PIPENV_VENV_IN_PROJECT=true
Starting Python project
Let us start Python project by the following commands:
mkdir project-name
cd project-name
pipenv --python 3.9.1
Change project-name
` to your own name. In this post, we use python 3.9.1, but you can choose any Python version here. Note that the Python version should be installed by pyenv. You can confirm the installed version by the following command:
pyenv versions
Installing development tools
Python project has been successfully created. Here I highly recommend to install the following development tools:
- flake8: https://flake8.pycqa.org/en/latest/
- mypy: https://mypy.readthedocs.io/en/stable/
- pytest: https://docs.pytest.org/en/stable/
Install the packages by the following command:
pipenv install --dev flake8 mypy pytest
flake8
is a package for code formatting. This is quite important when you work with many people. Just follow the code format.
It is quite important to detect errors before running the codes. For example, You run the code whose running time is 3 days, and after a day, runtime error is detected. Consequently, you lost whole day. We can prevent the tragedy by using mypy
and Python’s type hints. The detail can be checked in the documentation above.
It is preferable to write tests rather than to write some scripts directly. You can easily write tests by using pytest
.
Creating Makefile
It is convenient to create Makefile for automating the following procedure:
- check the code format (flake8)
- static type analysis (mypy)
- run the tests (pytest)
Suppose that the project directory is like this:
project-name/
project_name/
Pipfile
Pipfile.lock
Makefile
For this project directory, the Makefile is like this:
FLAKE8 ?= pipenv run flake8 --ignore=E501
MYPY ?= pipenv run mypy --pretty
PYTEST ?= pipenv run python -m pytest
all: flake8 mypy pytest
flake8:
$(FLAKE8) project_name
mypy:
$(MYPY) project_name
pytest:
$(PYTEST)
You can run the procedure by the following command:
make all
Whenever you change the codes, just execute the command above.
Conclusion
In this post, I introduce how to start Python project. I usually follow this procedure when starting Python project. Thanks for reading.
—
This post is linked to my Youtube channel:https://www.youtube.com/channel/UC-SHOllZQsQFCsbL3BvP1rA
The content might be updated in the future.