A quick guide on how to create a Python Wheel (executable pip package), when and why it can be very useful.
At times it becomes quite necessary to organize all your Python modules in nice and clean way for easy importing the underlying Python function / class in other codes. Doing this is relatively straight forward if using absolute, or relative import functionalities while working in your local system. However, it may not work like that if you are going to work in some cloud environment focused on notebook based coding, e.g. Databricks.
Python module is essentially a script generally consisting of some functions and/or classes, which can be referenced in other codes to make them concise, more readable, and easy to upgrade/enhance/maintain the modules — as all of it is kept in a single place.
Welcome Python Wheel (they say it is a newer version of Python Egg)!
Let me explain the usage and benefit of creating a wheel first :
- You can think of it as a way to make your set of modules import in any Python script, just like you do
import pandas as pd, orfrom matplotlib import pyplot as plt. - Once a Python Wheel is created, you can install it (file format with .whl extension) using simple
pip install [name of wheel file]. - You can upload all your modules, packages in one go with a single Python Wheel file in the cloud environment you are working on.
- Your existing scripts placed in local system where you were absolute/relative referencing your modules won’t have to be modified at all while migrating to a cloud environment like Databricks.
Now let’s quickly dive into how to create a very simple and basic wheel file with minimalist design. All you have to do is –
- Keep all the modules (python scripts), packages (folders/directories, which contain the modules) in a parent directory. Name the root directory whatever you like, typically something related to a project.
- Preferably, create an empty
.pyfile named__init__, and place this__init__.pyunder all the package directories and sub-package / sub-directories. No need to keep this in the root directory. Note that, this is not mandatory, but will be helpful. - Create a file named
setup.pyand place it in the root directory. Content of this script at the very minimal should include the distribution name, version number, and list of package names. An example below
https://medium.com/media/324b35a13f34292948cd6f27dc93555dSample setup.py configuration for creating a Python Wheel/Egg
Go to your command prompt/ conda prompt from where you can run python and pip commands, if not sure then check this link. Change directory in the command prompt and navigate to your project root directory where setup.py is placed . Execute python setup.py bdist_wheel. Voila!
A file with .whl extension will get created in a auto created sub-directory under the root, named dist. This is the Python Wheel 🙂
You can install this wheel on your local system to check how it works, just execute pip install <full file name of wheel file with .whl extension>, after navigating to dist directory.