Python wheel is an executable pip package. This allows organize all your Python modules in nice and clean way for easy importing the underlying Python function / class in other codes.
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.
Once a Python Wheel is created, you can install it (file format with .whl extension) using simple pip install [name of wheel file].
Below is steps to create simple and basic wheel file
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 .py file named __init__, and place this __init__.py under 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.py and 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
from setuptools import setup, find_packages
setup(
# this will be the package name you will see, e.g. the output of 'conda list' in anaconda prompt
name='ruleswhl',
# some version number you may wish to add - increment this after every update
version='1.0',
# Use one of the below approach to define package and/or module names:
# if there are only handful of modules placed in root directory, and no packages/directories exist then can use below syntax
# packages=[''], #have to import modules directly in code after installing this wheel, like import mod2 (respective file name in this case is mod2.py) - no direct use of distribution name while importing
# can list down each package names - no need to keep __init__.py under packages / directories
# packages=['<list of name of packages>'], #importing is like: from package1 import mod2, or import package1.mod2 as m2
# this approach automatically finds out all directories (packages) - those must contain a file named __init__.py (can be empty)
# include/exclude arguments take * as wildcard, . for any sub-package names
packages=find_packages(),
)
Now from the command line, enter the below to build the whl file
python setup.py bdist_wheel
The built whl file can be installed as usual using pip
pip install <whl file>
References
https://medium.com/swlh/beginners-guide-to-create-python-wheel-7d45f8350a94
No comments:
Post a Comment