Create your python module and publish it on pypi

When you've created a module you've put some effort into, and it feels useful for other developers, you probably want to share it. (Ahhh, developers really are altruistic creatures.)
Fortunately, for a Python project, it's pretty straightforward, just publish it on pypi (python package index), and other developers (thousands maybe?) will be able to install it with a simple `pip install`. It makes sharing your work super easy 🙂
Whether you made a handy little tool, a utility library, or a reusable component, here's how to turn it into a shareable python package.
Step 1: Basic structure of a python package
Before publishing anything, you need to organize your code so it's understandable and reusable (otherwise, there's not much point).
Good packaging starts with a clean file structure. That way, setuptools (or whatever build tool) knows how to assemble your module, and other developers can find their way around easily.
Create a folder like this:
your_module/
├── your_module/
│  └── __init__.py
├── tests/
│  └── test_basic.py
├── README.md
├── pyproject.toml
└── LICENSE
- `your_module/`: well, that's your code ^^
- `tests/`: unit tests (yep, even for personal projects — dev life is full of little obligations...). This lets you check that everything works before publishing. Releasing a buggy version would be kinda embarrassing oO
- `README.md`: description shown on pypi. you can totally use chatgpt to fix up spelling and grammar 😉
- `LICENSE`: license for your code (MIT, Apache 2.0, etc). without this, people won't know what they're allowed to do with it.
- `pyproject.toml`: project config. we'll get into what goes inside this file in the next part.
Step 2: Configure `pyproject.toml`
This file will hold your project's metadata (name, version, description, dependencies, etc.).
It replaces the old `setup.py` (RIP). It's essential if you want your project to be installed correctly, locally or via pypi.
[project]
name = "your-module-pythonium"
version = "0.1.0"
description = "an example python module published on pypi"
authors = [
 { name="your name", email="your@email.com" }
]
readme = "README.md"
license = {text = "MIT"}
dependencies = []
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
The name has to be unique on pypi, so you might need to add a prefix or suffix (like `monmodule-pythonium` or whatever).
Also, it's a good habit to tag versions (`version = "0.1.1"`, etc.) when you make updates. Otherwise, developers won't know what version they're using.
Step 3: Build the package
Make sure you have `build` installed:
pip install build
Then generate your package:
python -m build
You'll see two files appear in the `dist/` folder:
- a `.tar.gz` (source)
- a `.whl` (wheel, binary package)
These are the files you'll upload to pypi.
Step 4: Publish on pypi
create an account at https://pypi.org/account/register
Then install **twine** if you haven't already:
pip install twine
Publish:
twine upload dist/*
Twine will ask for your pypi credentials and upload your files.
If everything goes well, your package will be **publicly installable** via:
pip install your-module-pythonium
Conclusion
As you've seen, publishing a Python module on Pypi is really not that hard!
It's a great way to share your work with the Python community and contribute to the ecosystem 🙂
For more info: https://pypi.org/help/
Laisser un commentaire