How to push to PyPI your Python package
In this tutorial we will create a Python package and we will
automatically upload the resulting .tar.gz and .whl files to
the PyPI repository.
The steps were copied from
We assume you already created a repository on rocketgit.com, named
If you do not have an account on PyPI, go
and create one.
To be able to push your packages to PyPI, you need to create an API token.
and press Add API token
fill in any name you want, and, for Scope
Entire account (all projects)
and press Add token
Copy the API token to clipboard because you will need it later.
Clone your repository:
git clone https://rocketgit.com/prj1
Create a file named my_build_script.sh
with the following content:
# Build the package
python3 setup.py sdist bdist_wheel
# Upload package
python3 -m twine upload dist/*
It is the script which will get executed when you will run git push
Create another file, setup.py
. It is the build script for setuptools:
name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
description="A small example package",
long_description="bla bla bla",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
Create the LICENSE
Put here a nice license text.
Create the directory which will contain the code for your package:
Create the package main file, prj1.py
print("Hello from prj1!")
And the initialization file, __init__.py
from .prj1 import hello
We will add all the files to git:
git add .
git commit -m "First commit"
It is time to define a webhook which, when you do a git push, will
generate a job which will be sent to one of the workers.
The worker will start a build environment (a VM, for example)
and will run my_build_script.sh
(opens in a new tab)
or go to Settings
click on Webhooks
submenu and then press Add
From the list, go to type build
line and click "Generic".
or leave it empty to allow the webhook to execute for all
your projects. Add a description to easily identify the webhook.
Because we need to authenticate ourselves to PyPI, we need to set the API token:
go to Secrets
area, set the Name
field to TWINE_USERNAME
and the Value
field to __token__
Go to the next secret line and set the name to TWINE_PASSWORD
and, for the value,
paste the PyPI token you have in your clipboard. It must start with pypi-
event, select debian-10-amd64
environment, add python-setuptools
python-setuptools-wheel python3-setuptools python3-wheel twine
Packages to install
section (we add all the possible names to cover
all distributions - invalid names are ignored), and, for the first command,
input bash my_build_script.sh
. Finally, click Add
We can go back to terminal, and run:
If everything went OK, after few minutes, you can check the Webhooks area:
go to Settings
in the main menu and go to Webhooks
Scroll to your webhook, and expand Last output
area: here you will
find the output of the package installation process and the output of
script. From this log, you can figure out
why something went wrong or you can find that your upload was a success.
and you should see your package uploaded there.
To install your newly uploaded package, on any machine, use:
python3 -m pip install example-pkg-YOUR-USERNAME-HERE
To use it:
>>> import prj1