Creating Python packages
The pypa sample project contains a complete, easily modifiable template
setup.py that demonstrates a large range of capabilities setup-tools has to offer.
Every package requires a
setup.py file which describes the package.
Consider the following directory structure for a simple package:
__init__.py contains only the line
def foo(): return 100.
setup.py will define the package:
virtualenv is great to test package installs without modifying your other Python environments:
Making package executable
If your package isn't only a library, but has a piece of code that can be used either as a showcase or a standalone application when your package is installed, put that piece of code into
__main__.py in the
package_name folder. This way you will be able to run it directly from console:
If there's no
__main__.py file available, the package won't run with this command and this error will be printed:
python: No module named
package_name.__main__;'package_name' is a package and cannot be directly executed.
Uploading to PyPI
Setup a .pypirc File
This file stores logins and passwords to authenticate your accounts. It is typically stored in your home directory.
It is safer to use
twine for uploading packages, so make sure that is installed.
Register and Upload to testpypi (optional)
Note: PyPI does not allow overwriting uploaded packages, so it is prudent to first test your deployment on a dedicated test server, e.g. testpypi. This option will be discussed. Consider a versioning scheme for your package prior to uploading such as calendar versioning or semantic versioning.
Either log in, or create a new account at testpypi. Registration is only required the first time, although registering more than once is not harmful.
While in the root directory of your package:
Your package should now be accessible through your account.
Make a test virtual environment. Try to
pip install your package from either testpypi or PyPI.
If successful, your package is least importable. You might consider testing your API as well before your final upload to PyPI. If you package failed during testing, do not worry. You can still fix it, re-upload to testpypi and test again.
Register and Upload to PyPI
twine is installed:
Either log in, or create a new account at PyPI.
That's it! Your package is now live.
If you discover a bug, simply upload a new version of your package.
Don't forget to include at least some kind of documentation for your package. PyPi takes as the default formatting language reStructuredText.
If your package doesn't have a big documentation, include what can help other users in
README.rst file. When the file is ready, another one is needed to tell PyPi to show it.
setup.cfg file and put these two lines in it:
Note that if you try to put Markdown file into your package, PyPi will read it as a pure text file without any formatting.
It's often more than welcome to put a
LICENSE.txt file in your package with one of the OpenSource licenses to tell users if they can use your package for example in commercial projects or if your code is usable with their license.
In more readable way some licenses are explained at TL;DR.