As an Intermediate/Beginner Python developer you always wondered how to publish your own python packages so you can share it with your friends and colleagues. In this post I will be walk you through the process of publish your own packages on PyPi.

So What’s PyPi ?

PyPi is 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 and PyPi is itself open source and maintained by developers in there free time and that makes PyPi great.

Lets begin writing our first Package.

This is going to be a simple python package that will calculate square and cube of a number passed as a commandline arguement.

In order to get started we need.

  • Python installed
  • pip install twine this will allow us to connect to pypi and publish our package.
  • pip install docopt this package will parse our arguements passed as commandline arguements.

Lets begin writing code.


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

    -h --help     Show this screen.

from docopt import docopt 

def square(num):

def cube(num):

if __name__ == '__main__':                                                                                                                              
  arguments = docopt(__doc__)
  if arguments['square']:
  elif arguments['cube']:
  • so after writing our simple package we need to create a file named This file will allow us to install package and it is going to be used by PyPi.
  • Furthermore, We need to create a README file that will contain instructions regarding installion and usage of the package.

  • This is how our file is going to look like and this will contain details regarding our package.

import setuptools

with open("", "r") as fh:
    long_description =

    author="Your Name",
    author_email="Your Email",
    description="Description regarding the package",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",

  • After creating a now we will run this command to generate our package .tar file.
python3 sdist

  • This is going to create a folder named dist/ which will contain our package and now we will upload our package using twine.
twine upload dist/
# This will prompt username and password for pypi

Finally you have published your first PyPi Package.

I hope you enjoyed this post if you think i missed any thing feel free to dm me on twitter Feel to share it among your friends and colleagues.