How to publish an Open-Source Python package to PyPI repository
Packaging Python Projects in Python 3.11
For example I create this library:
How did I publish this repository? I’ll tell you in a moment.
Structure of project:
myproject-source-dir/
├── LICENSE
├── pyproject.toml
├── README.md
├── magic_config/
│ └── magic_config/
│ ├── __init__.py
│ └── lib.py
├── .gitignore
├── tests/
└── setup.py
Cat pyproject.toml
[build-system]
requires = [
"setuptools>=42",
"wheel",
]
build-backend = "setuptools.build_meta"
[project]
name = "magic-config"
version = "0.1.5"
authors = [
{ name = "Alexander Majorov", email = "alexander.majorov@gmail.com" },
]
description = "A simple library"
readme = "README.md"
requires-python = ">=3.10.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
"Homepage" = "https://github.com/frontdevops/magic-config"
"Documentation" = "https://github.com/frontdevops/magic-config/blob/main/README.md"
"Bug Tracker" = "https://github.com/frontdevops/magic-config/issues"
For convenience, so as not to make edits to multiple files, I made it so that the file setup.py
takes all the data from the file pyproject.toml
Cat setup.py
Generate distribution archives and upload to PyPi
Make sure you have the latest versions of setuptools
and wheel
installed:
pip install --user --upgrade setuptools wheel
Now run this command from the same directory where setup.py
is located:
python -m build
python -m twine upload --verbose dist/*
For convenience, you can write your login in the file ~/.pypirc
:
[testpypi]
username = your-username[pypi]
username = your-username
Install your own package using pip
Now everyone can install your package with familiar pip install
command:
pip install {your_package_name}
for update use:
pip install {your_package_name} --upgrade
This is a simple guide to my work flow, how I publish my projects.