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="contact@gfg.com",
packages=["test_package"],
description="A sample test package",
long_description=description,
long_description_content_type="text/markdown",
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"))
print(count_in_list(["gfg", "dsa", "gfg"], "maths"))
|
Output:

So that was all to developing a package and deploying it to PyPI.
Similar Reads
How to Build a Python package?
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, tensor
5 min read
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 Inst
5 min read
How to Install a Python Package with a .whl File?
In this article, we are going to see how we can install a Python Package with a .whl file. Users must have Python 3.x (any version like 3.9,3.10 etc) installed on their device and may download any .whl file of their choice prior to following this article. The user should also need to verify that pip
4 min read
How to Make API Call Using Python
APIs (Application Programming Interfaces) are an essential part of modern software development, allowing different applications to communicate and share data. Python provides a popular library i.e. requests library that simplifies the process of calling API in Python. In this article, we will see ho
3 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 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
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
Python Packages
We usually organize our files in different folders and subfolders based on some criteria, so that they can be managed easily and efficiently. For example, we keep all our games in a Games folder and we can even subcategorize according to the genre of the game or something like that. The same analogy
14 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
How to Check If Python Package Is Installed
In this article, we are going to see how to check if Python package is installed or not. Check If Python Package is InstalledThere are various methods to check if Python is installed or not, here we are discussing some generally used methods for Check If Python Package Is Installed or not which are
5 min read
Top 10 Python Packages to Learn in 2024
Python is one of the most popular programming languages which is used by more than 80% of the developers. Top Python packages offer some amazing features like easy to learn and understand, enhanced security and performance. It consists of modules, packages, and libraries that play a major role in ke
6 min read
How to Install a Python Module?
A module is simply a file containing Python code. Functions, groups, and variables can all be described in a module. Runnable code can also be used in a module. What is a Python Module?A module can be imported by multiple programs for their application, hence a single code can be used by multiple pr
4 min read
Add packages to Anaconda environment in Python
Let's see some methods that can be used to install packages in the Anaconda environment. There are many ways one can add pre-built packages to an anaconda environment. So, let's see how to direct the path in the anaconda and install them. Add packages to the Anaconda environment using the navigator
2 min read
Create and Access a Python Package
Packages are a way of structuring many packages and modules which helps in a well-organized hierarchy of data set, making the directories and modules easy to access. Just like there are different drives and folders in an OS to help us store files, similarly packages help us in storing other sub-pack
5 min read
How to uninstall a package using python setup.py?
When working with Python, managing packages is a common task. Typically, package managers like pip are used for installing and uninstalling packages. However, in some cases, you might encounter packages installed via a setup.py script, particularly in development or custom environments. This guide w
3 min read
How to Install SimpleJson Package for Python?
In this article, we will see how to Install SimpleJson Package for Python SimpleJson is an open-source package in python. It is a fast, simple, correct, and extensible JSON encoder and decoder for python. Python's built-in JSON library has been externally developed to provide backward compatibility
2 min read