Source package ~wheel way

Introduction to Wheel in Python

Wheel is a python wrapper that packages your software’s dependencies, libraries, and dependencies so that they can be easily installed each time they are used without recompiling.

Wheel of the official document is wheel. Readthedocs. IO/en/latest /.

In its use, there are two limitations: 1. The implementation of the bdist_wheel command in setupTools must be an extension of setupTools Bdist_wheel setuptools command) 2. This package command can only be run in a command window. A command line tool for working with wheel files

Setuptools official documentation setuptools. Readthedocs. IO/en/latest /.

Help information about the wheel command

(pkg) michaelkoo@MacBook much % wheel
usage: wheel [-h] {unpack,pack,convert,version,help} ...

positional arguments:
  {unpack,pack,convert,version,help}
                        commands
    unpack              Unpack wheel
    pack                Repack wheel
    convert             Convert egg or wininst to wheel
    version             Print version and exit
    help                Show this help

optional arguments:
  -h, --help            show this help message and exit
Copy the code

Wheel packaging in Python

Compile the source code for python modules

  • Create source koo. Py and add the following to koo.
def ko():
    print('it is ko invoke')

def mm():
    print('it is mm invoke')

name = 'mm ko'
Copy the code
  • In the source directory, in addition to the source code, you need to have the setup.py file, in which you enter configuration information:
From setuptools import setup setup(name='KaKaHelper',version='1.0.1',py_modules=['koo'],description='A helper for KaKa',author='KaKa',author_email='[email protected]',license='MIT',keywords='ka = [' helper ', install_requires scapy > = 2.4.4 ', 'psutil > = 5.8.0'], python_requires = '> = 3'),Copy the code
  • The setup.py parameter is referenced
Name: indicates the file name of the packaged package. Version: indicates the version number, which is added as the suffix name of the packaged file. Author: author author_email: indicates the email address of the author. Include_package_data: Include_package_data and package_data are specified for projects that have non-PY files such as HTML and JS. Package_data: normally written as {' your_package_name': [" files "]}, include_package_data is not finished, you also need to modify the manifest.in file. The syntax of the manifest. in file is as follows: include XXX/XXX/XXX /.ini/(all files ending in. Ini can also be named.) license: supported open source protocol Description: A short description of the project is ext_modules: it is a list of Extension instances, and the Extension definition has some parameters. Ext_package: defines the relative path of the extension requires: defines which modules provide the extension requires: Defines which modules can provide dependent data_files: Specifies other files (such as configuration files) that specify which files are installed in which directories. If the directory name is a relative path, it is a path relative to sys.prefix or sys.exec_prefix. If no template is provided, it is added to the MANIFEST file.Copy the code
  • Input the README. Md
# KaKaHelper
This is helper for kaka
Copy the code
  • Create a set of py

Type the following in __init__.py:

from .koo import *
Copy the code
  • Contents of the current directory:
README.md	__init__.py	koo.py		setup.py
Copy the code
  • After entering the configuration information, open the terminal in the current directory and run the following command to generate the compressed package:
python setup.py sdist
Copy the code

After the operation is successful, the following information is displayed:

running sdist running egg_info writing requirements to KaKaHelper.egg-info/requires.txt writing KaKaHelper.egg-info/PKG-INFO writing top-level names to KaKaHelper.egg-info/top_level.txt writing dependency_links to KaKaHelper.egg-info/dependency_links.txt reading manifest file 'KaKaHelper.egg-info/SOURCES.txt' writing manifest file 'KaKaHelper.egg-info/SOURCES.txt' running check warning: check: missing required meta-data: Url Creating KakaHelper-1.0.1 creating KakaHelper-1.0.1 / Kakahelper-egg-info Copying files to KakaHelper-1.0.1... Copying Readme. md -> Kakahelper-1.0.1 imitate koo. Py -> KakaHelper-1.0.1 imitate setup.py -> Kakahelper-1.0.1 imitate Kakahelper.egg-info/pkg-info -> Kakahelper-1.0.1 / kakahelper.egg-info copying kakahelper.egg-info/sources.txt -> KaKaHelper - / KaKaHelper 1.0.1. An egg - info copying KaKaHelper. An egg - the info/dependency_links. TXT - > Kakahelper-1.0.1 / Kakahelper.egg-info copying kakahelper.egg-info -> Kakahelper-1.0.1 / kakahelper.egg-info Copying Kakahelper.egg-info /top_level.txt -> Kakahelper-1.0.1 / kakahelper.egg-info Writing Kakahelper-1.0.1 /setup.cfg Creating Dist Creating tar Archive Removing 'KaKaHelper-1.0.1' (and everything Under It)Copy the code

In the current directory, dist directory will also be generated, which contains the following contents:

(pkg) michaelkoo@MacBook kaka % ls dist 
KaKaHelper-1.0.1.tar.gz
Copy the code

So far, it has been packaged in the form of a source distribution package.

  • Use the following command to create the wheel installation package
 python setup.py bdist_wheel
Copy the code

The running results are as follows:

running bdist_wheel running build running build_py creating build creating build/lib copying koo.py -> build/lib Installing to build/bdist. Macosx-10.15-x86_64 /wheel running install Running install_lib creating Build /bdist. Macosx-10.15-x86_64 Creating build/bdist. Macosx-10.15-x86_64 /wheel Copying Build /lib/koo. Build /bdist. Macosx-10.15-x86_64 /wheel running install_egg_info Running egg_info writing requirements to KaKaHelper.egg-info/requires.txt writing KaKaHelper.egg-info/PKG-INFO writing top-level names to KaKaHelper.egg-info/top_level.txt writing dependency_links to KaKaHelper.egg-info/dependency_links.txt reading manifest file 'KaKaHelper.egg-info/SOURCES.txt' writing manifest file 'KaKaHelper.egg-info/SOURCES.txt' Copying Kakahelper. egg-info to build/bdist. Macosx-10.15-x86_64 /wheel/ kakahelper-1.0.1-py2.7.egg -info Running install_scripts Creating the build/bdist. Macosx - 10.15 - x86_64 / wheel/KaKaHelper - 1.0.1. Dist - info/wheel creating 'dist/ kakahelper-1.0.1-py2-none-any. WHL 'and adding 'build/bdist. Macosx-10.15-x86_64 /wheel' to it adding 'koo 'KaKaHelper - 1.0.1. Dist - info/METADATA,' adding 'KaKaHelper - 1.0.1. Dist - info/WHEEL' adding 'KaKaHelper - 1.0.1. Dist - info/top_level. TXT' adding 'KaKaHelper - 1.0.1. Dist - info/RECORD' o Build/bdist. Macosx - 10.15 - x86_64 / wheelCopy the code

The WHL installation package is now available in Dist:

michaelkoo@MacBook kaka % ls dist Kakahelper-1.0.1-py3-none-any. WHL kakahelper-1.0.1.tar.gzCopy the code

Wheel installation in Python

The wheel installation package is generated by following the steps above, so let’s install it now:

  • First find the WHL directory
  • Check if KaKaHelper is installed in the current environment.
(pkg) michaelkoo@MacBook kaka % pip list
Package    Version
---------- -------
pip        21.0.1
psutil     5.8.0
scapy      2.4.4
setuptools 53.1.0
wheel      0.36.2
Copy the code

You can see from the above output that KaKaHelper is not installed in the current environment

  • Use the following command to install:
PIP install - no - index KaKaHelper - - py2 - none - any 1.0.1. WHLCopy the code

The installation result is as follows:

Processing./ kakahelper-1.0.1-py3-none-any. WHL Requirement already satisfied: Scapy > = 2.4.4 in/Users/michaelkoo/work/env/PKG/lib/python3.7 / site - packages (from KaKaHelper = = 1.0.1) (2.4.4) Requirement already satisfied: Psutil > = 5.8.0 in/Users/michaelkoo/work/env/PKG/lib/python3.7 / site - packages (from KaKaHelper = = 1.0.1) (5.8.0) Installing Collected Packages: KaKaHelper Successfully installed KaKaHelper-1.0.1Copy the code
  • Check the library installation for the current environment again:
(pkg) michaelkoo@MacBook kaka % pip list
Package    Version
---------- -------
KaKaHelper 1.0.1
pip        21.0.1
psutil     5.8.0
scapy      2.4.4
setuptools 53.1.0
wheel      0.36.2
Copy the code

KaKaHelper 1.0.1 is installed.

Verifying the Installation

To verify the installation, create a new test.py file and enter the following:

from koo import *
ko()

print(name)
Copy the code

The running results are as follows:

(pkg) michaelkoo@MacBook pkg % python test.py 
it is ko invoke
mm ko
Copy the code

At this point, the wheel has been completed from packaging to installation, and then to use.

General error

ERROR:is not support

ERROR: Kakahelper-1.0.1-py2-none-any. WHL is not a supported wheel on this platform.Copy the code

Solution:

By default, the Python environment is 2.x and python3.x in my current environment, so this installation is not suitable for my current environment. Also, the name of the installation [py2] also indicates the environment. If you need to specify the Python environment, You can use [python_requires='>=3'] in the setup.py file.Copy the code