Publishing packages on PyPI

As an intermediate/beginner Python developer, you are always wondering how to publish your own Python packages so that you can share them with your friends and colleagues. In this post, I will be walking you through the process of publishing your own packages on PyPI.

So what’s PyPI?

PyPI stands for Python Package Index. It’s more like npm or Homebrew where you can find different packages such as Flask, Django, Tweepy, and much more. Most of the packages are open sourced; PyPI itself is open source and maintained by developers in their free time. That’s what makes PyPI great.

Let’s begin writing our first package

This is going to be a simple Python package that will calculate the square and cube of a number passed as a command-line argument.

In order to get started, we need:

  • Python
  • pip install twine - this will allow us to connect to PyPI and publish our package.
  • pip install docopt - this package will parse the arguments we passed in the command-line.

Let’s begin writing code:

"""calc

Usage:
    calc.py square <num>
    calc.py cube  <num>
    calc.py (-h | --help)

Options:
    -h --help     Show this screen.

"""
from docopt import docopt 


def square(num):
  print(num**2) 

def cube(num):
  print(num**3)


if __name__ == '__main__':                                                                                                                              
  arguments = docopt(__doc__)
  if arguments['square']:
    square(int(arguments['<num>']))
  elif arguments['cube']:
    cube(int(arguments['<num>']))
  • So, after writing our simple package, we need to create a file named setup.py. This file will be used by PyPI and will allow others to install the package.

  • Furthermore, We need to create a README file that will contain instructions regarding installation and usage of the package.

  • This is how our setup.py file is going to look. It will contain details regarding our package.

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="Calculator",
    version="0.0.1",
    author="Your Name",
    author_email="Your Email",
    description="Description regarding the package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="Project_url",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

  • After creating a setup.py, we need to run the following command to generate our package .tar file.
python3 setup.py sdist

  • This is going to create a folder named dist/ that will contain our package; then, we will upload our package using twine.
twine upload dist/
# This will prompt username and password for PyPI

All done!

Finally, you have published your first PyPI Package.

I hope you enjoyed this post. If you think I missed anything, feel free to DM me on Twitter. Also, feel free to share this among your friends and colleagues if you find it useful.

Attributions:
  1. undefined by undefined