Pip Package Management System

Karan Jakhar
3 min readNov 1, 2019

--

#day8 of #100daysofcode

I was curious about how the packaging system works in Python. I wanted to understand it so that I can create my own package. Distribution is a very important part of development. If we are not able to distribute our work in a format that others can easily install and use then it is of less use to others. Python packages and modules are the powerhouses. They are the reason people are using it so much. There is a library for every kind of use case. That’s why Python is so widely used language. There is a lot of hard work put by many people to develop so awesome libraries. I decided to learn about the Pip package management system and how to bind up a project in a package to share it with others. I encourage you to try it once. It also clears the concept of importing the modules.

I will explain everything in brief. How I created a sample package and installed it in my own system.

The first step is to create and file in a structured way.

Created a folder “sample_package_kj” with mkdir command. As I use Ubuntu it is easy for me to create files and folders from the terminal. Then in the folder, I created three files __init__.py, birds.py, and mammals.py. Other files come after we compile these files, we need not create them.

__init__.py is used for initializing the package. We can keep it empty. birds.py and mammals.py are our two modules under package sample_package_kj. There could be many sub-packages under this package.

Outside of the sampla_package_kj, I created four files setup.py, test.py, README.md, and LICENSE.txt

setup.py contains basic details about the package like author name, description, etc. I used test.py to run my modules and check that all are working fine.

You have to fill in the basic details like above. After that, it is just running a few commands in the terminal and your package is ready.

sudo python -m pip install --upgrade pip setuptools wheel

This will install the required packages. After this go to your package folder and run the following command.

python setup.py sdist bdist_wheel

This will create a wheel format file and many other files. To install this newly created package in your system. Type the following command in your package folder.

pip install dist/sample_package_kj-0.0.1-py3-none-any.whl

After dist/<your package name> should be put. Above is my package.

After doing all this you can import your package in your code. Following is the final tree structure of my files. If you are wondering how to create this tree structure. It is very easy to do in Ubuntu. I only know how to do this in it. For windows, I have no idea. You have to install a “tree” first in Ubuntu. This can be done by running “sudo apt install tree”. Then just type “tree” and you are good to go.

GitHub Link:

https://github.com/karanjakhar/100-days-of-code/tree/master/day8/python_packages_kj

References:

Happy Learning !!!

--

--

Karan Jakhar

Generative AI | Computer Vision | Deep Learning | Blogger | Technology Enthusiast