Python > Deployment and Distribution > Packaging Python Projects > Publishing Packages to PyPI or private registries
Packaging and Publishing a Simple Python Package to PyPI
This example demonstrates how to package a simple Python project and publish it to the Python Package Index (PyPI). It includes steps for creating the necessary files, building the package, and uploading it to PyPI using `twine`.
Project Structure
Before packaging, organize your project in a logical directory structure. A typical structure looks like this: my_package/ ├── my_package/ │ ├── __init__.py │ └── my_module.py ├── pyproject.toml ├── README.md ├── LICENSE `my_package/my_package/` contains the actual Python code. `pyproject.toml` specifies build requirements. `README.md` describes the package. `LICENSE` specifies the license.
Creating the `pyproject.toml` File
The `pyproject.toml` file specifies the build system requirements. This example uses `setuptools` and `wheel`. This file is crucial for modern Python packaging and replaces the older `setup.py` method.
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
Example Python Module (`my_package/my_module.py`)
This is a simple example Python module containing a function `greet` that takes a name as input and returns a greeting string.
def greet(name):
return f"Hello, {name}!"
Creating `__init__.py`
The `__init__.py` file makes the directory a Python package. It can be empty, but here we import `greet` from `my_module` to make it directly accessible from the top-level package (e.g., `my_package.greet`).
# my_package/__init__.py
from .my_module import greet
Building the Package
Use the `build` module to create distribution packages (wheel and source distribution). Run this command from the root directory of your project (the directory containing `pyproject.toml`). This will generate files in the `dist/` directory.
python -m build
Installing `twine`
`twine` is a tool for securely uploading Python packages to PyPI.
pip install twine
Uploading to PyPI
Use `twine` to upload the generated packages to PyPI. You will be prompted for your PyPI username and password. Consider using an API token instead of your password for better security. You'll need to register your project on PyPI first.
twine upload dist/*
Real-Life Use Case
Imagine you have developed a utility library for data processing or a custom machine learning algorithm. Packaging and publishing it to PyPI allows other developers to easily install and use your code in their projects using `pip install my_package`.
Best Practices
Interview Tip
Be prepared to discuss the benefits of packaging, the role of `pyproject.toml`, and the importance of using `twine` for secure uploads during an interview. Explain why you chose a specific build backend and how it impacts the packaging process.
When to use packaging
Package your Python code when you want to:
Alternatives
Alternatives to using PyPI for distribution are:
Pros of packaging
Cons of packaging
FAQ
-
What is the purpose of `pyproject.toml`?
The `pyproject.toml` file specifies the build system requirements for your Python project. It tells the build tools how to build your package, including which dependencies are needed during the build process. -
Why use `twine` instead of directly uploading with `setup.py`?
`twine` uses HTTPS and provides a more secure way to upload your packages to PyPI, protecting your credentials during the upload process. `setup.py upload` is deprecated. -
How do I specify dependencies for my package?
Dependencies are specified in the `install_requires` section of the `setup()` function (if using setup.py) or within the `pyproject.toml` file when using a build system like setuptools with declarative configuration. -
What is a wheel file?
A wheel is a distribution format for Python packages that is designed to be easily installed. It is a pre-built archive that avoids the need to compile code during installation, making the process faster and more reliable.