DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Managing AWS Managed Microsoft Active Directory Objects With AWS Lambda Functions
  • Multiplatform Directory Bookmarks on the Command Line
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Implementing ROS Communication Patterns

Trending

  • Give Your AI Assistant Long-Term Memory With perag
  • Native SQL in Java Without JDBC Boilerplate — Meet Ujorm3
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • From "Vibe Coding" to Production: Setting Up an Evals Loop for Claude Agents
  1. DZone
  2. Coding
  3. Languages
  4. Packaging Python Classes as a Wheel

Packaging Python Classes as a Wheel

Let's get this wheel rolling.

By 
Bipin Patwardhan user avatar
Bipin Patwardhan
·
Apr. 03, 21 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
9.3K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Recently, while working on a project where we were using Python, I realized how easy the Java package mechanism makes it, to organize and reuse common classes and functions.

Due to my Java hangover, I organized the common classes and functions in a directory hierarchy like com.<company>.<project>.util. The util package contained a few classes for UUID creation as well as classes for date and time manipulation. After placing the files in the above-mentioned directory, I started developing the application as a micro-service. I placed the micro-service code in the directory com.<company>.<project>.<service>. While trying to use the date and time classes in the micro-service, I used the syntax from com.<company>.<project>.util.mydatetime import MyDateTime.

This syntax threw a compilation error, as the Python environment was not able to find the com/company/project/util directory. I was puzzled by this and after some time realized that the Python environment was expecting the MyDateTime class in the com/company/project/service/com/company/project/util directory. Compared to this, when the package path is mentioned in each Java class, the Java environment resolves the reference to the class.

After some exploration, I settled on packaging the common classes as a wfheel. Once a package is made into a wheel, it can be installed like any other Python package, using the pip command. Upon installation, the package is installed in the same place where external site packages are installed using pip.

The advantage of a wheel is that after installation, we can refer to the class as com.<company>.<project>.util from anywhere in the code and it will be resolved. The limitation is that each time the common code is updated, we have to repackage the wheel and distribute it to all users. Each user has to uninstall the existing wheel and then install the updated wheel.

As we were in the development phase, we had to frequently update the wheel and make it available to other members of the development team. To make the task of building and managing the wheel, I wrote a few Windows batch files. In the remaining part of the article, I am sharing the batch files. I also have a few helper batch files that make my task of file management a bit easier.

Note: Though the code snippet mentions PowerShell, the commands have to be executed on Windows Command Prompt by saving each one into a .bat file

Delete Backup Files - del-bak.bat

PowerShell
 




xxxxxxxxxx
1


 
1
@echo off
2
echo Deleting .bak files
3
del /S *.bak


Delete Dist Directory - del-dist.bat

Before creating the actual wheel, I thought it better to delete the 'dist' directory that is referred by Python during wheel creation. This ensures that files that are no longer part of the project do not continue to be included in the wheel.

PowerShell
 




xxxxxxxxxx
1
10


 
1
@echo off
2
call set-package-name.bat # rem sets the value for the PACKAGE_NAME variables
3
echo Cleaning old files and directories
4
del /Q /S dist
5
rd /Q /S dist
6
del /Q /S build
7
rd /Q /S build
8
del /Q /S %PACKAGE_NAME%.egg-info
9
rd /Q /S %PACKAGE_NAME%.egg-info
10
echo Done


Delete Log Files - del-log.bat

PowerShell
 




xxxxxxxxxx
1


 
1
@echo off
2
echo Deleting .log files
3
del /S *.log *.log.*
4
echo Done


Make Wheel - make-wheel.bat

Before building the wheel, delete the 'dist' directory, set the package name as environment variable, and then invoke a command to make the wheel.

PowerShell
 




xxxxxxxxxx
1


 
1
@echo off
2
call del-dist.bat
3
call set-package-name.bat
4
echo Creating new distribution
5
python setup.py bdist_wheel
6
echo Done
7
pause


Install the Wheel - wheel-install.bat

PowerShell
 




xxxxxxxxxx
1


 
1
@echo off
2
call set-package-name.bat
3
echo Installing %PACKAGE_NAME% package
4
pip install dist\%PACKAGE_NAME%.whl
5
echo Done
6
pause


Uninstall the wheel - wheel-uninstall.bat

PowerShell
 




xxxxxxxxxx
1


 
1
@echo off
2
call set-package-name.bat
3
echo Uninstalling %PACKAGE_NAME% package
4
pip uninstall dist\%PACKAGE_NAME%.whl
5
echo Done
6
pause


Run the Steps in Sequence - refresh.bat

I packaged the various steps related to wheels into a helper batch file. The file uninstalls the existing package and makes the new wheel. Once the wheel is created, it is installed.

PowerShell
 




xxxxxxxxxx
1


 
1
call package-uninstall.bat
2
call del-dist.bat
3
call make-wheel.bat
4
call package-install.bat


Set Package Name - set-package-name.bat

Set the name of the package for use by other batch files.

PowerShell
 




xxxxxxxxxx
1


 
1
@echo off
2
set PACKAGE_NAME=my_package
3
set INSTALL_NAME=%PACKAGE_NAME%-1.0.0


setup.py

While the batch files make the task of creating and installing the wheel easy, the key to making the wheel is the setup file.

Python
 




xxxxxxxxxx
1
22


 
1
#setup(name="my_package", version="1.0", packages=find_packages())
2

          
3
with open("README.md", "r") as fh:
4
    long_description = fh.read()
5

          
6
setuptools.setup(
7
    name="my_package",
8
    version="1.0",
9
    author="myself",
10
    author_email="[email protected]",
11
    description="my package",
12
    long_description=long_description,
13
    long_description_content_type="text/markdown",
14
    url="https://myself.myself/my_package",
15
    packages=setuptools.find_packages(),
16
    classifiers=[
17
        "Programming Language :: Python :: 3",
18
        "License :: OSI Approved :: MIT",
19
        "Operating System :: OS Independent"
20
    ],
21
    python_requires=">=3.7"
22
)


Conclusion

By creating these helper files, each time I make changes to the code and test it, I simply run refresh.bat and all the work is done one by one. I only need to keep pressing the 'Enter' key a couple of times - while it is not required, I use it to keep track of the steps.

Python (language) PowerShell Directory

Opinions expressed by DZone contributors are their own.

Related

  • Managing AWS Managed Microsoft Active Directory Objects With AWS Lambda Functions
  • Multiplatform Directory Bookmarks on the Command Line
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Implementing ROS Communication Patterns

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook