Python Project Setup
See my updates in 2024 on setting up a Python project
Recently I have split some python projects into separate packages,
rather than git submodules. In an effort to make these packages useful
to myself in a variety of situtations, I want to make the packages
available on PyPI and ensure they run on
multiple versions of python (at least the versions I have installed on
my local machine–3.6 to 3.9). flit makes it easy
to publish packages to PyPI if there’s nothing complicated going on
(like compiling C extensions or bundling JavaScript). Then I use tox as a generic
command runner to run tests on multiple versions of Python, as well as
check that code is formatted and has correct static types. This is a
living document of the process I use to set up or convert some existing
code to a standalone Python package.
Project-Specific
- (Only if you use pyenv) Set up pyenv to use as many versions as you
want to test with tox:
pyenv local 3.9.12 3.8.11 3.7.12 3.6.15. The first version should be the one you want to use for your virtual environment. - Make a new virtual environment. It doesn’t matter what version of Python you use–I use the latest version I have installed on my machine.
pip install flitflit init. Answer the questions.pip install tox- Make your
tox.inifile.pyproject.tomlis supported by tox but only as a single string, so for now we are stuck withtox.ini. I hav an exampletox.iniat the end of this post. - Install all your dependencies in your virtual environment
(
pip install pytest black isort mypy # etc.) and develop and write tests as normal. - Run
toxand it will create the virtual environments then run the tests, type checks and formatting checks.
Additional Tips
Make isort follow black’s conventions:
[tool.isort]
profile = "black"Add coverage to pytest:
pip install pytest-cov
[tool.pytest.ini_options]
addopts = "--cov prelude --cov-report html"Example tox.ini File
The isolated_build = True is required for
pyproject.toml-based projects.
[tox]
envlist =
py3{9,8,7,6}
typing
formatting
isolated_build = True
[testenv]
deps =
pytest
hypothesis
pytest-cov
commands =
pytest
[testenv:typing]
deps =
mypy
commands =
mypy --strict prelude
[testenv:formatting]
deps =
isort
black
commands =
isort --quiet --check .
black --quiet --check .Sam Stevens, 2024