You should follow the tutorial Packaging Python Projects and first, I explain how to create, upload and install a package for TestPyPI and lastly, I explain it for PyPI:
First, create packaging_tutorial
folder:
packaging_tutorial/
Then, create a virtual environment in packaging_tutorial
folder:
For Linux and Mac:
python -m venv venv && . venv/bin/activate
For Windows:
python -m venv venv && . venv/Scripts/activate
Then, venv
folder is created:
packaging_tutorial/
└-venv/ # Here
Then, upgrade pip
:
pip install --upgrade pip
Then, create src/example_package_<YOUR_USERNAME_HERE>/__init__.py
(Empty file) and example.py
and tests
folder(Empty folder) . *Replace <YOUR_USERNAME_HERE>
with your username, for example, example_package_superkai
in my case:
packaging_tutorial/
|-src/
| └-example_package_<YOUR_USERNAME_HERE>/
| ├──__init__.py # Empty file
| └──example.py # Here
|-tests/ # Empty folder
└-venv/
Then, put the code below to example.py
:
def add_one(number):
return number + 1
Then, create pyproject.toml
, LICENSE
and README.md
:
packaging-tutorial/
|-src/ # ↓ ↓ ↓ Here ↓ ↓ ↓
| └-example_package_<YOUR_USERNAME_HERE>/
| |-__init__.py
| └-example.py
|-pyproject.toml # Here
|-README.md # Here
|-LICENSE # Here
|-tests/
└-venv/
Then, put the code below to pyproject.toml
. *Replace <YOUR_USERNAME_HERE>
with your username:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project] # ↓ ↓ ↓ Here ↓ ↓ ↓
name = "example_package_<YOUR_USERNAME_HERE>"
version = "0.0.1"
authors = [
{ name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
"Homepage" = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"
Then, put the text below to README.md
:
# Example Package
This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.
Then, put the text below to LICENSE
:
Copyright (c) 2018 The Python Packaging Authority
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Then, upgrade and run build:
pip install --upgrade build && python -m build
Then, dict/...whl
and ...tar.gz
are created. *<YOUR_USERNAME_HERE>
is replaced with your username:
packaging-tutorial/
|-dist/ # ↓ ↓ ↓ Here ↓ ↓ ↓
| ├-example_package_<YOUR_USERNAME_HERE>-0.0.1-py3-none-any.whl
| └-example_package_<YOUR_USERNAME_HERE>-0.0.1.tar.gz
|-src/ # ↑ ↑ ↑ Here ↑ ↑ ↑
| └-example_package_<YOUR_USERNAME_HERE>/
| |-__init__.py
| └-example.py
|-pyproject.toml
|-README.md
|-LICENSE
|-tests/
└-venv/
Next, create an account and API token in TestPyPI. *Select Entire account (all projects) for Scope:

Then, an API token is created:

Next, install and upgrade twine:
pip install --upgrade twine
Then, upload example_package_<YOUR_USERNAME_HERE>
package to TestPyPI. *Input __token__
for username and the API token for password:
twine upload --repository testpypi dist/*
Then, example_package_<YOUR_USERNAME_HERE>
package is uploaded to TestPyPI:

Next, install example_package_<YOUR_USERNAME_HERE>
package from TestPyPI:
pip install -i https://test.pypi.org/simple/ example-package-<YOUR_USERNAME_HERE>
Then, you can use example.add_one()
from example_package_superkai
package(module):
$ python
>>> from example_package_superkai import example
>>> example.add_one(2)
3
Lastly, how to create, upload and install a package for PyPI is really similar to TestPyPI but there are some differences so for PyPI:
Without --repository testpypi
, upload example_package_<YOUR_USERNAME_HERE>
package to PyPI:
`twine upload dist/*`
Without -i https://test.pypi.org/simple/
, install example_package_<YOUR_USERNAME_HERE>
package from PyPI:
pip install example-package-<YOUR_USERNAME_HERE>