Skip to content

Instantly share code, notes, and snippets.

@cfbastarz
Created December 8, 2023 19:25
Show Gist options
  • Save cfbastarz/ea3a57c367e73caf9e12db3ec2106d33 to your computer and use it in GitHub Desktop.
Save cfbastarz/ea3a57c367e73caf9e12db3ec2106d33 to your computer and use it in GitHub Desktop.
Publish a simple package to PyPi

Publish a simple package to PyPi

A recipe on how to publish a simple package (i.e., pure Python) to PyPi.

Setup your ~/.pypirc

First, setup your ~/.pypi to make things a little easier:

[distutils]
index-servers =
    pypi
    testpypi

[pypi]
repository = https://upload.pypi.org/legacy/

[testpypi]
repository = https://test.pypi.org/legacy/

This way, it is possible to use twine to upload by using just testpypi instead of https://test.pypi.org/legacy/.

Creating the wheel 🛞

Go to the source directory where your setup.py is and run:

cd MyPackage
python setup.py bdist_wheel sdist

This will create a .tar.gz with the source code and a .whl, which is a binary package. Check the packages created:

twine check dist/*

If something fails, you can fix it and check it again until everything PASS.

Uploading the package

Upload first you package to https://test.pypi.org and test the installation. This is a very important step because it is when you will fix problems related to package dependencies:

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

And test the installation:

pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ MyPackage

Notes:

  1. Test the installation of you package in virtual environments:

    conda create -n MyPackage python=3.x.y
    conda activate MyPackage
    pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ MyPackage

    or:

    python -m venv MyPackage
    source MyPackage/bin/activate
    pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ MyPackage
  2. The options --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ are important since they will allow pip to download dependencies packages from both https://test.pypi.org/ (where MyPackage is) and https://pypi.org (where probably the dependencies are).

Once everythings works as it should, publish it to the official PyPi.

Public publishing

Use twine to publish MyPackage to https://pypi.org:

twine upload dist/*

And now MyPackage can be easely installed with:

pip install MyPackage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment