How to Upload a Package to PyPI
How to Upload a Package to PyPI
This guide explains how to upload a Python package to PyPI using pyproject.toml
, build
, and twine
.
Prepare Your Project Structure
A typical Python package structure looks like this:
1 |
|
Key Notes:
- The PyPI package name (e.g., my-package) uses hyphens.
- The importable Python name (e.g., my_package) uses underscores.
- A single file
my_package.py
instead of a directorymy_package/
containing__init__.py
is also OK.
Create a pyproject.toml
File
Here's an example pyproject.toml
(using setuptools
as the build backend):
1 |
|
Install Required Tools
First, install build
and twine
:
1 |
|
Build the Package
Run the following command in your project's root directory:
1 |
|
This generates .tar.gz
and .whl
files in the dist/
folder.
Upload to PyPI
Use twine
to upload your package. Navigate to the dist/
directory and run:
1 |
|
You'll be prompted for your PyPI API token. Refer to the official PyPI documentation for details.
Verify Publication
After uploading, check PyPI to see if your package is listed: https://pypi.org/
Search for your package name-it may take a few minutes to appear.
How to Upload a Package to PyPI
https://jifengwu2k.github.io/2025/05/28/How-to-Upload-a-Package-to-PyPI/