How to Build a Python package?
Last Updated :
30 May, 2021
In this article, we will learn how to develop the package in Python. Packages are nothing but a collection of programs designed to perform a certain set of task(s). Packages are of two types, namely
- Built-in Packages like collection, datetime, sqlite, etc.
- External packages like flask, django, tensorflow, etc.
Creating a package
First, we need to think of a way to structure our code, so that others can access our code functionalities. In Python, to make a package, we need to add an __init__.py to the directory. Here, we are going to make a package called test_package.
- Let's write the __init__.py
Python3
from collections import Counter
def count_in_list(l, word):
c = Counter(l)
return c[word]
- Now, create a directory called test_package, and in it keep the __init__.py file. That's all, our package is ready. All our package does count the number of occurrences of a word in a list. Now, to use our package, create a run.py file outside the test_package directory. Inside the run.py simply import the newly created package and use the count_in_list function. We can write the code as shown below.
Python3
from test_package import count_in_list
l = ["gfg", "dsa", "gfg"]
count = count_in_list(l, "gfg")
print(count)
- That's all. We have just created our first package. Our directory structure should look something like this
package-folder
├── run.py
└── test_package
└── __init__.py
1 directory, 2 files
To test it out, simply type python3 run.py. And our output should be as follows:
In this manner, we can create many complex programs and use them in other codes.
Uploading our package
Now, let's see how we can prepare our package to be deployed on PyPI. We need some additional files in our package folder, like README.MD, LICENSE, and setup.py. Our code should be structured as below.
package-folder
├── LICENSE
├── README.md
├── setup.py
└── test_package
└── __init__.py
1 directory, 4 files
Make sure to remove the previously created run.py, it was intended for manual testing only. For the LICENSE we would recommend going with the MIT LICENSE as it offers the most flexibility. We can read up about various types of licenses online.
Next, create the README.md file. It would essentially contain a complete description of the package. If we are new to markdown-style writing, we recommend reading this article. Once, we have your README.md file ready, we need to write the setup.py. This is the most important part.
Python3
import setuptools
with open("README.md", "r") as fh:
description = fh.read()
setuptools.setup(
name="test-package",
version="0.0.1",
author="GeeksforGeeks",
author_email="[email protected]",
packages=["test_package"],
description="A sample test package",
long_description=description,
long_description_content_type="text/markdown",
url="https://github.com/gituser/test-tackage",
license='MIT',
python_requires='>=3.8',
install_requires=[]
)
In the above code, you need to change the
- author with your name
- author_email with your email
- url with your GitHub URL of the package
Our package is now ready.
Register our package to PyPI
Now that we developed our python package, we need to register it on PyPI.
1. Upload to GitHub
Create a new GitHub repository and push all our code there. If you don't know how to push code to a GitHub repo, you can head over and read this article. Also, do not forget to update our URL in setup.py with the newly created GitHub repo URL. Our repo should be publicly accessible.
2. Create an account in PyPI
We are going to publish the package in PyPI, we need an account. To do that, simply visit PyPI and create your account.
3. Generating distributions
Distribution archives are needed for hosting it as a package. To generate these packages, we need to install two additional packages.
pip3 install setuptools wheel
Now that we installed them, run the below command to generate archives:
python3 setup.py sdist bdist_wheel
It will generate folders build, dist, test_package.egg-info. Now your directory structure should look something like this.
package-folder
├── build
│ ├── bdist.linux-x86_64
│ └── lib
│ └── test_package
│ └── __init__.py
├── dist
│ ├── test_package-0.0.1-py3-none-any.whl
│ └── test-package-0.0.1.tar.gz
├── LICENSE
├── README.md
├── setup.py
├── test_package
│ └── __init__.py
└── test_package.egg-info
├── dependency_links.txt
├── PKG-INFO
├── SOURCES.txt
└── top_level.txt
7 directories, 11 files
4. Deploy
To upload to PyPI we need the twine package installed.
pip3 install twine
Now, upload the package with the proper version defined in the setup.py file. To do so, run the below command
twine upload --repository pypi dist/*
That was all about developing and deploying packages in python.
Using our package
Once the package is developed, we should be able to use it right? After all, it was developed to reuse the same logic at different codebases. Create a completely new directory and create a virtual environment inside it. To so, simply type
python3 -m venv env
source env/bin/activate
Now, install your newly deployed package to the virtual environment. You need to use pip to be able to install it.
pip install test-package
Now, let's see how we can use this package in our code. The idea is simple. This is after all a package, right? So, all we need to do is import it as a package.
from test_package import count_in_list
Now, we have our function. Let's try to use it.
print(count_in_list(["gfg", "dsa", "gfg"], "gfg")) # output: 2
print(count_in_list(["gfg", "dsa", "gfg"], "maths")) # output: 0
That's all. If we put it all together,
Python3
from test_package import count_in_list
print(count_in_list(["gfg", "dsa", "gfg"], "gfg")) # output: 2
print(count_in_list(["gfg", "dsa", "gfg"], "maths")) # output: 0
Output:
So that was all to developing a package and deploying it to PyPI.
Similar Reads
How to Build and Publish Python Packages With Poetry Poetry is a modern and useful tool for package development and distribution in Python that helps with dependency management. The entire process is consolidated into a single, intuitive interface with Poetry, in contrast to conventional methods that call for numerous tools and intricate setups. This
6 min read
How To List Installed Python Packages Working on Python projects may require you to list the installed Python packages in order to manage dependencies, check for updates, or share project requirements with others. In this post, we'll look at numerous techniques for listing the Python packages that are installed on your system.List Insta
5 min read
How to Install a Python Package with a .whl File? To install a Python package using a .whl (wheel) file, you'll need to follow a few simple steps. The first method involves using PowerShell along with pip and the cd command to change the directory to where your .whl file is located. The second method uses PowerShell and pip directly, without changi
4 min read
How to Install Python Package in Google's Colab Installing a Python package in Google Colab is like going on a space adventure with your keyboard as a trusty spaceship. Don't worry, fellow coder, the world of Python packages is ready for your exploration. with just a few lines of code, you can easily bring in the tools for your coding adventure.
3 min read
How to Manually Install Python Packages? Python is one of the most famous and powerful languages in the world. It is a dynamically typed, high-level interpreted language intended for general use. Python first made its public appearance in the year 1991, which means it is a fairly old language. It was designed by Guido Van Rossum and develo
4 min read
How to Install Packages in Python on Linux? To install a package in python, we use pip. The pip is a python package manager. In this tutorial, we will be discussing how we can install packages in python on a Linux system. To install packages in python on Linux, we must have python and pip installed on our Linux machine. As python comes preins
2 min read
How to Install python 'bottle package' on Linux? py-bottle is a lightweight micro-framework for developing small web apps and it supports request dispatching (Routes) with URL parameter support, templates, a built-in HTTP Server, and adapters for several third-party WSGI/HTTP-server and template engines are all included in a single file with no de
2 min read
How to Install Packages in Python on MacOS? To get started with using pip, you should install Python on your system. Make sure that you have a working pip. Installing Packages in Python on MacOS: Follow the below steps to install python packages on MacOS: Step 1: As the first step, you should check that you have a working Python with pip inst
2 min read
Python Packages Python packages are a way to organize and structure code by grouping related modules into directories. A package is essentially a folder that contains an __init__.py file and one or more Python files (modules). This organization helps manage and reuse code effectively, especially in larger projects.
12 min read
Importlib package in Python In this article, we are going to know about the Importlib package in the Python programming language. The importlib package is primarily utilized by Python applications for dynamic imports during runtime. In layman's terms, it allows the user to load modules as he/she discovers them. This package ha
6 min read