To propose a well-maintained and usable Python package to the open-source community or even inside your company, you are expected to accomplish a set of critical steps. First ensure that your code is unit tested. Second respect the common writing and format styles. Automate these steps and integrate them in a continuous integration pipeline to avoid any regression that stems from modifications applied to your source code. Finally, provide enough documentation for future users. Once done it is common to publish your Python package on the Python Package Index (PyPI). Here we are going to see how to accomplish each of these steps using Poetry, Tox and GitHub Actions. The code used for our use case can be found on our repository.
Automate linter checks and tests with tox
If not done, activate your virtual environment.
1 | poetry shell |
To check the conformity of our code, we use a couple of packages that are going to evaluate if the code respects the common Python writing guidelines. Then, to automate their execution as well as our unit tests, we use tox. To install them run:
1 | poetry add black flake8 pylint tox --dev |
tox and poetry don’t work well together by default. They are somewhat redundant. To use them together, we need to implement a few tricks (see issues 1941 and 1745). tox install its own environment and dependencies to run its tasks. But to install dependencies, you have to declare the command poetry install in our tox configuration. This brings redundancy and can lead to some issues. Moreover, this does not allow to install developers dependencies needed to execute our tests. It is more productive to let tox use the poetry.lock file to install necessary dependencies. For this, I advise you to use the tox-poetry-installer package developed to solve these problems:
1 | poetry add tox-poetry-installer[poetry] --dev |
Now we declare our tox configuration in a tox.ini file whose content is:
1 | [tox] |
You can see two sections here:
[tox]: Define the global settings for yourtoxautomation pipeline including the Python version of the test environments.[testenv]: Define the test environments. In our case we have some extra-variablesrequire_locked_depsandinstall_dev_depsthat are brought by the tox-poetry-installer package.require_locked_depsis to choose whether or not you wanttoxto harness thepoetry.lockfile to install dependencies.install_dev_depsis to choose iftoxinstalls the developer dependencies.
Refer to the
toxdocumentation to learn more about the configuration as well as thetox-poetry-installerdocumentation to learn more about it extra configuration.
Run the tox pipeline:
1 | tox |
An error is raised because pylint shed light on some style inconsistencies. By default, tox quits if any warnings or errors occurred during the execution of the commands. The errors are by themselves quite explicit. After correcting them, run again the pipeline:
1 | tox |
Perfect. The tox automation pipeline succeed locally. The next step start implements the CI pipeline with GitHub Actions.
Continuous Integration with GitHub Actions
GitHub Actions make it easy to automate all your software workflows. This service is event-driven meaning that a set of commands is triggered when a specific event occurs. Such events could be a commit pushed to the branch or a pull request. GitHub Actions are pretty convenient to run all needed tests against your code.
Most importantly, GitHub Actions provide the ability to test your Python package using several Python versions and on different operating systems (Linux, macOS and Windows). The only thing you need is an existing repository and a .github/workflows/<file_name>.yaml file:
1 | mkdir -p .github/workflows |
The content of the .github/workflows/ci.yml file is:
1 | name: CI Pipeline for summarize_df |
A few words about the different fields:
on: this field defines the type of event that is going to trigger the pipeline.jobs: this field defines the multiple steps of your pipeline. They run in an instance of a virtual environment.build: this is where all the magic happens:- The
strategy.matrix.platformfield defines the different OS you want to use to test your package. Use templating to pass these values to thebuild.runs-onfield (${{matrix.platform}}). - The
strategy.matrix.python-versionfield defines the different versions of Python you want to use to test your package. - The
stepsfield permits you to specify which actions you use (steps.uses) and which command you want to run (steps.run)
- The
Before finishing, alter the tox.ini and pyporject.toml files accordingly. Initially we chose the 3.8 Python version for tox. But we want it to be compatible with 3.7 and 3.9. For the pyproject.toml file, choose a version expected to be compatible with your package. Here we choose to make our package compatible from 3.7.1 and above. Below are the changes added to our files:
1 | # content of: tox.ini |
Having several python version defined in your
tox.inifile causes issue with your local testing. Running thetoxraises an error because of lacking python versions. If you still want to test you module locally just use thetox -e pycommand.
1 | # content of: pyproject.toml |
When you modify the
pyproject.tomlfile, always run thepoetry updatecommand that can check some unexpected incompatibilities between your dependencies and the version of Python you wish to use.
To finish, we are going to install a package, called tox-gh-actions, to run tox in parallel on GitHub while using several versions of Python:
1 | poetry add tox-gh-actions --dev |
The pipeline is ready. Add, commit and push your changes to see the pipeline running:
1 | echo "!.github/" >> .gitignore |
Go to your GitHub repository and click on the Actions tab:

You see all the previous and ongoing pipelines:

Let’s click on the ongoing pipeline. The pipeline runs on each OS and for each Python version. Wait a couple of minutes to see the results:

All the pipelines succeed! We are ready to publish our package on the PyPi registry.
Publish packages on PyPi with poetry
To make your package publishable, add some details in the [tool.poetry] section of your pyproject.toml file:
1 | [tool.poetry] |
All the variables here are quite explicit. These are metadata needed for the publication of the package. The include variable is interesting to add any files you want. In our case we are going to add a CHANGELOG.md file. Do you remember commitizen? If not please take the time to read our article on commitizen and conventional commits. Use the following command:
1 | cz bump |
It prints the semantic version from your pyproject.toml file and ask you to create a Git tag. The version will be updated based on your Git commit. Next we create the CHANGELOG.md:
1 | cz changelog |
Your CHANGELOG.md has been created based on the Git history you generated thanks to commitizen. Pretty neat isn’t it?! Once done let’s focus on publishing our package:
1 | poetry build |
This creates a folder called dist where the built package is located. To test if everything works you can use pip:
Do this outside of your virtual environment to not pollute it.
1 | pip install path/to/your/package/summarize_dataframe-0.1.0-py3-none-any.whl |
Now we need to create an account on PyPi. Just enter the expected details, validate your email and execute:
1 | poetry publish |
The package is now online and shared with the community.

Conclusion
tox provides a nice interface to automate all your unit tests and validation checks. The ecosystem around poetry is getting more mature and provides solutions to work with tox without too much hassle. Collectively, these two solutions permit to establish a very efficient and coherent CI pipeline. To run the pipeline and test your packages against different OS or versions of Python, you can leverage GitHub Actions as described above.
poetry was at the center of our approach. From the project initialization to its publication and going through the management of its packages and dependencies. poetry demonstrated its ease of use and efficacy that will definitely facilitate the life of developers, Data Scientists or Data Engineers who develop projects in Python.
Our articles describe a full setup that you can leverage to build your own Python project to respect good software engineering practices.
Cheat Sheet
tox
Run your tox pipeline
1
tox
poetry
Build your package
1
poetry build
Publish your package
1
poetry publish
Acknowledgments
This article was first published in Adaltas blog and kindly reviewed by the CEO David Worms and one consultant Barthelemy NGOM.