Python: Create and upload your project to PyPI library

Python: Create and upload your project to PyPI library

Python Package Index (PyPI)

This post covers the basic of how to build and distribution your own Python package and upload it to Python package index (PyPI) library in order to the other develops of the end-users can install and use it easily. It is not difficult and everyone can do it! Follow the below instruction step-by-step, when it is done, you and other people can type just pip install your_project to install your package!

1. Prerequisite packages

  • Python 3.x
python --version
  • setuptools
  • wheel
  • twine
pip install setuptools wheel twine --upgrade --user

2. First of all, you should have

  • Project source code
  • Github repository (optional, but highly recommended)
    • LICENSE
    • README.md
  • Test-PyPI account (optional, but highly recommended)
  • PyPI account

3. Creating configuration file

  • setup.py
#!/usr/bin/env python

import setuptools

__version__ = "x.x.x"                                # version of your package

with open("README.md", "r", encoding="utf-8") as f:
    long_description = f.read()

description = "This is my project"                   # description of package

setuptools.setup(
    name="YOUR_PACKAGE_NAME",                        # your package name
    version=__version__,
    author="PACKAGE_NAME",                           # author name
    author_email="AUTHOR_EMAIL",                     # author e-mail
    description=description,
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="LINK_TO_WEBSITE",                           # link to project website
    download_url="RELEASE_FILE",                     # link to release archive
    project_urls={
        'Documentation': 'PROJECT_DOCS',             # link to package document
        'Source': 'PROJECT_SOURCE_CODE',             # link to project source code
    },
    packages=setuptools.find_packages(),
    install_requires=[
        'numpy'                                      # list of package dependencies, fx, numpy
    ],
    classifiers=[
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Development Status :: 5 - Production/Stable",
        "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
        "Operating System :: OS Independent",
        "Intended Audience :: Developers",
        "Natural Language :: English",
    ],
    python_requires='>=3',
)

4. Building distribution files

Build *.tar.gz source code archive and *whl distribution files using command:

python setup.py sdist bdist_wheel

Both *tar.gz and *whl will be built and stored in a ./dist directory.

5. Upload package to server

Test-PyPI

  • Upload file
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
  • It will ask you to enter your PyPI account, for example:
Enter your username: rangsiman
Enter your password: (invisible)
Uploading distributions to https://test.pypi.org/legacy/
Uploading octadist-2.5.3-py3-none-any.whl
100%|████████████████████████████████████| 51.3k/51.3k [00:03<00:00, 14.5kB/s]
Uploading octadist-2.5.3.tar.gz
100%|████████████████████████████████████| 33.4k/33.4k [00:01<00:00, 20.7kB/s]


PyPI

  • Upload file
twine upload dist/*
  • It will ask you to enter your PyPI account, for example:
Enter your username: rangsiman
Enter your password: (invisible)
Uploading distributions to https://upload.pypi.org/legacy/
Uploading octadist-2.5.3-py3-none-any.whl
100%|████████████████████████████████████| 51.3k/51.3k [00:03<00:00, 14.5kB/s]
Uploading octadist-2.5.3.tar.gz
100%|████████████████████████████████████| 33.4k/33.4k [00:01<00:00, 20.7kB/s]

6. Searching Package

Test-PyPI

pip search octadist --index https://test.pypi.org/pypi


PyPI

pip search octadist --index https://pypi.org/pypi

or

pip search octadist

7. Show package detail

pip show octadist

Sample output:

Name: octadist
Version: 2.5.3.1
Summary: OctaDist: A tool for calculating distortion parameters in coordination complexes.
Home-page: https://octadist.github.io
Author: Rangsiman Ketkaew
Author-email: rangsiman1993@gmail.com
License: UNKNOWN
Location: c:\xxx\xxx\xxx\anaconda3\lib\site-packages
Requires: matplotlib, rmsd, numpy, scipy
Required-by:

8. Installing

Test-PyPI

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


PyPI

pip install octadist

Rangsiman Ketkaew