Skip to content

Instantly share code, notes, and snippets.

@cfbastarz
Last active August 21, 2024 17:34
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 guide on how to publish a simple package (i.e., pure Python) to PyPi.

Set Up Your ~/.pypirc

First, set up your ~/.pypirc 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, you can use twine to upload by simply specifying 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 file with the source code and a .whl file, which is a binary package. Check the created packages:

twine check dist/*

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

Uploading the Package

First, upload your package to https://test.pypi.org and test the installation. This is a very important step because it allows you to fix problems related to package dependencies:

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

Then, 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 your 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 because they allow pip to download dependency packages from both https://test.pypi.org/ (where MyPackage is) and https://pypi.org (where the dependencies are likely located).

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

Publishing

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

twine upload dist/*

Now, MyPackage can be easily installed with:

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