Hướng dẫn create whl file python
Show
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.Photo by Jon Cartagena on UnsplashWhen 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 Environmentpip install virtualenv /* Install virtual environment */ File StructureEnsure 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 FilesBelow 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__.pydef function_init(): 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.pyfrom testWheel.__init__ import * def func_test(): 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.
setup.pyBefore 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 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 fileType 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 projectI 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 * Below is the output Successfully Imported Init.py ConclusionThe 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
I hope this article was able to help you out 🙂 |