Publishing to pypi

Yesterday I published something to pypi for the first time in a while:

https://pypi.org/project/fakespotipy/

(it's a fake spotipy library for helping write python unit tests without having to actually call Spotify's API)

pypi-logo

The Python Package Index (PyPI) is a repository of software for the Python programming language.

Currently, when you search Google for "publish to pypi", this is the top post:

http://peterdowns.com/posts/first-time-with-pypi.html

but it contains some outdated/deprecated information. Much of the information is still useful, like writing your setup.py file, and registering for accounts on PyPI and PyPI Test, but when it comes to actually publishing, there are a few changes.

This tutorial is actually much better:

https://packaging.python.org/tutorials/distributing-packages/

TL;DR

Here's the short version/my notes:

1. setup.py

You'll need to create a setup.py file for your project. Here's the one I made yesterday:

https://github.com/rcrdclub/fakespotipy/blob/master/setup.py

2. Twine

You'll need this twine library to publish.

pip install twine

3. Source distribution

You have to create a source distribution:

python setup.py sdist

4. Wheel

It's good to create a wheel for your project, for faster end-user installation (faster than building from source).

For this you'll need wheel:

pip install wheel

There are three types of wheels, Universal, Pure Python, and Platform.

See the wheels section of the docs for further info on which type you need.

Mine was universal, so I did:

python setup.py bdist_wheel --universal

5. Uploading

The article talks about setting up a $HOME/.pypirc file so you don't have to enter your credentials everytime, but this seems insecure to me, so I skipped it.

To upload your source distribution (and potentially wheels) to Test PyPI:

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

And to go to real PyPI:

twine upload dist/*