DEV Community

Cover image for How to deploy your Python Package(library) in Production?
chetan jain
chetan jain

Posted on

How to deploy your Python Package(library) in Production?

Package or Library

Package or library is a collection of logical related classes, functions and methods that allows you to perform many actions without writing your code.
We can reuse code whenever we want from the library. We don't have to write it again. A library can be installed from the python package manager like pip (preferred installer program) or conda.

If you want to install any python package then just go on anaconda prompt and type

pip install <package name>

Pip install the package from the python package index (PyPI). It is also known as the third party library repository for python. It hosts all python library in the form of archives called 'sdist' (Source Distributions) or pre-compiled wheels.

So if we want our package in production so we have to upload our package to PyPI. First, we have to check whether our package is working fine or not.

So first upload it on test.PyPI and then if everything goes well we will upload it on main PyPI repository.

Register on test.PyPI.org and PyPI.org.

Steps to Post your code on test.PyPI repository.

  • First make your package ready including init.py file.
  • Second inside the package along with all modules folder you have to create a setup.py file.
# In the setup.py file 
from setuptools import setup

setup(name= <name of your package>, # NOTE it should be unique.
      version=0.1,
      description='Guassian and binomial distributions', # Small descriptions.
      author = 'Chetan jain' , # Optional in test.PyPI.
      author_email = 'chetanjain645@yahoo.com', # Optional in test.PyPI.
      license ='MIT', # Optional in test.PyPI.
      packages=['distributions'], #include all package in the list.
      zip_safe=False) # It safe your file to directly install from the zip.
  • Third install twine library from the anaconda prompt.

pip install twine

Now we are ready to upload package on test.PyPI repository.

  • Open Cmd and go to package folder and Type commands.

python setup.py sdist

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

  • Now just type your email and password for test.PyPI.org.

  • Now try to install it. If everything goes well then you made it.

pip install --index-url https://test.pypi.org/simple/ <your package name>

If you made it install in your system now we are ready to upload your package on PyPI.org.

  • Uninstall your test package which you install

pip uninstall <your package name>

  • Update setup.py
from setuptools import setup

setup(name= <name of your package>, # NOTE it should be unique.
      version=0.1,
      description='Guassian and binomial distributions', # Small descriptions.
      author = 'Chetan jain' , # Author name .
      author_email = 'chetanjain645@yahoo.com', # Author email
      license ='MIT', # Choose a valid license.
      packages=['distributions'], #include all package in the list.
      zip_safe=False) # It safe your file to directly install from the zip.
  • Choose a license from this website and make a new file license.txt along with setup.py file and copy your license detail from the website and paste it on license.txt file.

  • Make a README.md file and tell about your python library in that.

  • Make a setup.cfg file and paste this.

[metadata]
description-file = README.md 

Now we are all set. Now just type the following command.

twine upload dist/*

Now try to install it.

pip install <your package name>

If this goes well.
Congratulations you made it so far and uploaded your package to the PyPI repository.

If you don't know about anaconda distributions go to this website.

Note that pypi.org and test.pypy.org are two different websites. You'll need to register separately at each website. If you only register at pypi.org, you will not be able to upload to the test.pypy.org repository.
Also, remember that your package name must be unique. If you use a package name that is already taken, you will get an error when trying to upload the package.

Thanks for reading.

Discussion (0)