How to build and publish your own Python package to PyPi library?
Introduction to PyPi
Before we start we should have knowledge of what PyPi is all about.
Python Package Index (which is abbreviated as PyPi) is a repository of software for the Python programming language. We can find and install any open python package in our system with the use of the pip. All the open python packages can be found here: https://pypi.org/
There are more than 220,665 projects ready to install from PyPi library.
Installing a Python package
Installing a python package is pretty simple with the help of the pip tool.
The pip command is a tool for installing any Python packages which are mostly found in the Python Package Index. Your system needs to have the pip installed inorder to install any python package.
Installation of pip:
sudo apt-get install python-pip
After pip is installed in the system, search for the python package to install in their library and install using their pip install command:
pip install <package_name>
Now we know how to install a python package, let’s learn how to create a python package.
Creating a Python Package
There are prerequisites to install before we proceed. These are:
- Setuptools: It is a package development process library designed to create and distribute Python packages.
pip install setuptools
- Wheel: The Wheel package provides a bdist_wheel command for the setuptools. It creates a .whl file which is directly installable through the pip install command.
pip install wheel
- Twine: The Twine package provides a secure, authenticated, and verified connection between your system and PyPi over HTTPS.
pip install twine
Setup the project for Python package
Create a setup file setup.py in your package. This file must contain all your package metadata information.
Following is an example of a setup file:
import setuptools with open("README.md", "r") as fh: long_description = fh.read() setuptools.setup( name='<project_name>’, version='0.1', author="Deepak Kumar", description="<project_description>", long_description=long_description, long_description_content_type="text/markdown", packages=setuptools.find_packages(), )
Compiling the package
Execute the following command to compile the python package:
python setup.py bdist_wheel
After compiling the package, it will create following structure:
- build: build package information.
- dist: Contains your .whl file. A WHL file is a package saved in the Wheel format, which is the standard built-package format used for Python distributions. You can directly install a .whl file using pip install some_package.whl on your system
- project.egg.info: An egg package contains compiled bytecode, package information, dependency links, and captures the info used by the setup.py test command when running tests.
Create a PyPi account and upload to PyPi library
You need to have a PyPi account to upload to its library. Upload the dist/*.whl file on PyPi by using Twine:
python -m twine upload dist/*
You will be asked to provide the credential to upload with the PyPi account.
Verify the PyPi package
Create a virtual environment of Python 3.
virtualenv -p python3 <env_name>
Activate the created virtual environment to install the package:
Install the python package which you uploaded in the PyPi library:
pip install <package_name>
It’s good to go. Implement and use the functions of the package in your project.