If you have worked with Python you must be familiar with the pip command used to install packages. This article will show you how to create a wheel file for your custom packages and import it in other projects.

When you use pip to install modules or packages, you must have unknowingly installed a few wheel files as well. A wheel file is similar to a zip file in many ways, you compress all your python files and dependencies into a single file. You can use this file in different projects or the cloud. Installing a wheel file is as simples as installing a package using pip. They can also be helpful when you are collaborating with others or when you need to deploy your projects.
Setup Virtual Environment
pip install virtualenv /* Install virtual environment */
virtualenv venv /* Create a virtual environment */
venv/Scripts/activate /* Activate the virtual environment */
File Structure

Ensure your Python files are inside a single folder. The name of the folder will be the name you will use in ‘ import ‘ statements in other projects. You can also have sub-folders inside. The setup.py file must be outside your package folder and must be at the same level as the venv folder and the testWheel folder.
The Python Files
Below are my python files and their content, although an init.py file is not necessary to have, it is a good practice to have one. If you are importing files locally from the folder or files inside your folders are importing other files inside the folder, you will need an init.py file. It doesn’t matter if it is a blank file as long as it is present.
init.py
def function_init():
print('Successfully Imported Init.py')
def print_age(age):
print(f'I am {age} years old')
It has two functions, the first one simply prints a statement to the console and the second one takes an input and prints it to the console.
test.py
from testWheel.__init__ import *
def func_test():
print("Successfully Imported test.py file")
def print_name(name):
print(f'Hello {name}')
def print_test_age(age):
print_age(age)
First, we import the functions from the init.py file cause why not 😃
We have two functions similar to our previous file. We also have a third function which uses the print_age function from our previous file.
Ensure your import path follows a similar format as above, you might encounter an error if you have a statement like ‘ from init.py import * ‘
setup.py
Before creating this file, you will need to install a couple of Python packages.
pip install wheel setuptools
Below is the content of the setup.py file
from setuptools import setup
setup(
name='testWheel',
version='1.0',
packages=['.testWheel'],
)
The name parameter contains the name of your package, the version is the version number. The packages parameter is a list containing the names of your packages (the folder which contains your .py files). For my case, I have my files in a single folder, therefore I only need to list down the one folder’s name. If you want to include multiple folders, you will need to list them down as well.
Create the Wheel file
Type the following command to create the Wheel File
python setup.py bdist_wheel --universal
Remove the universal tag if you want to create a Pure-Python Wheel.
Go to this website to learn more about the different types of wheels. For this article, I’ll be creating a universal wheel.
After the command finishes executing successfully, a folder name ‘dist’ will be created. This folder contains the .whl file which is your wheel file.
Importing custom package in a new project
I have created a new folder and a new virtual environment in it.

Copy your wheel file and paste it into the directory of the new project. Ensure your virtual environment is activated and use the following command to install your wheel file.
pip install testWheel-1.0-py2.py3-none-any.whl
If you used a different name earlier, change the statement accordingly.
Below is the content of my main.py file which imports the wheel file
from testWheel.__init__ import *
from testWheel.test import *
'''
Functions from __init__.py
'''
function_init()
print_age(21)
'''
Functions from test.py
'''
func_test()
print_name('World')
print_test_age(34)
Below is the output
Successfully Imported Init.py
I am 21 years old
Successfully Imported test.py file
Hello World
I am 34 years old
Conclusion
The output is as expected! We have successfully created a wheel file and imported it. Some things to keep in mind while creating the wheel file
- Ensure your file is structured properly
- Ensure all your python files are included in the wheel file. After typing the command to create the wheel file, you should see output messages similar to ‘added ./…py file’
- Ensure your import paths are correct
- Update your version number if you want to install the wheel file in a project where it has already been installed. If the version number remains unchanged, pip won’t install it.
I hope this article was able to help you out 🙂
I recently started a modified version of the #100daysofcode challenge. I aim to write content related to Python, data science, or programming every day.
Follow me on LinkedIn, Twitter
https://realpythonproject.com on November 25, 2020.