python的神奇功能二、源碼打包

語言: CN / TW / HK

源碼打包~wheel方式

python的wheel介紹

wheel是python中的一個打包方式,它可以為你軟件的依賴庫或依賴軟件提供打包,它打包的依賴包可以在每次使用都可以很方便的安裝而不需要再次編譯。

wheel的官方文檔是https://wheel.readthedocs.io/en/latest/

在它的使用過程中,有2個限制的地方: 1.提供給“setuptools”中的“bdist_wheel”命令的實現必須是“setuptools”的拓展 (A setuptools extension for building wheels that provides the bdist_wheel setuptools command) 2.這個打包命令只能在命令窗口中運行。 (A command line tool for working with wheel files)

setuptools官方文檔https://setuptools.readthedocs.io/en/latest/.

wheel命令的相關幫助信息

``` (pkg) [email protected] 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 ```

python 的wheel打包方式

整理源碼為python模塊

  • 新建源碼koo.py,在koo.py添加以下內容: ``` def ko(): print('it is ko invoke')

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

name = 'mm ko' ```

  • 在源碼目錄中,除了源碼之外,還需要有setup.py文件,在setup.py文件中輸入配置信息: ``` 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'), ```

  • setup.py參數參考 ``` name : 打包起來的包的文件名

version : 版本號,添加為打包文件的後綴名

author : 作者

author_email : 作者的郵箱

py_modules : 打包的.py文件

packages: 打包的python文件夾

include_package_data : 項目裏會有一些非py文件,比如html和js等,這時候就要靠include_package_data 和 package_data 來指定了。package_data:一般寫成{‘your_package_name': [“files”]}, include_package_data還沒完,還需要修改MANIFEST.in文件.MANIFEST.in文件的語法為: include xxx/xxx/xxx/.ini/(所有以.ini結尾的文件,也可以直接指定文件名)

license : 支持的開源協議

description : 對項目簡短的一個形容

ext_modules : 是一個包含Extension實例的列表,Extension的定義也有一些參數。

ext_package : 定義extension的相對路徑

requires : 定義依賴哪些模塊

provides : 定義可以為哪些模塊提供依賴

data_files :指定其他的一些文件(如配置文件),規定了哪些文件被安裝到哪些目錄中。如果目錄名是相對路徑,則是相對於sys.prefix或sys.exec_prefix的路徑。如果沒有提供模板,會被添加到MANIFEST文件中。 ```

  • 輸入README.md ```

KaKaHelper

This is helper for kaka ```

  • 創建__init__.py 在__init__.py中輸入以下內容: from .koo import *

  • 當前目錄的內容: README.md __init__.py koo.py setup.py

  • 輸入配置信息之後,在當前目錄中,打開終端輸入如下命令生成壓縮包文件: python setup.py sdist

操作成功後,會有如下提示信息: ``` 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 copying koo.py -> KaKaHelper-1.0.1 copying setup.py -> KaKaHelper-1.0.1 copying KaKaHelper.egg-info/PKG-INFO -> KaKaHelper-1.0.1/KaKaHelper.egg-info copying KaKaHelper.egg-info/SOURCES.txt -> KaKaHelper-1.0.1/KaKaHelper.egg-info copying KaKaHelper.egg-info/dependency_links.txt -> KaKaHelper-1.0.1/KaKaHelper.egg-info copying KaKaHelper.egg-info/requires.txt -> 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) ```

當前目錄下,也會生成dist目錄,在dist中包含有以下內容: (pkg) [email protected] kaka % ls dist KaKaHelper-1.0.1.tar.gz

截止到目前為止,以【源代碼發佈包】的形式已打包完成。

  • 使用如下命令創建wheel安裝包 python setup.py bdist_wheel 運行結果如下: 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.py -> 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 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.py' adding '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' removing build/bdist.macosx-10.15-x86_64/wheel

現在在dist中就已經有了whl安裝包: [email protected] kaka % ls dist KaKaHelper-1.0.1-py3-none-any.whl KaKaHelper-1.0.1.tar.gz

python 的wheel安裝方式

按照以上步驟,我們生成好了wheel安裝包,那我們現在來安裝: - 首先找到whl目錄 - 然後檢查下,當前環境有沒有安裝【KaKaHelper】 ``` (pkg) [email protected] 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 ``` 從以上輸出可知,當前環境中沒有安裝KaKaHelper

  • 使用如下命令進行安裝: ``` pip install --no-index KaKaHelper-1.0.1-py2-none-any.whl

安裝結果如下: 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.1 - 再次查看當前環境的庫安裝情況: (pkg) [email protected] 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 ``` 可以看到已經安裝好【KaKaHelper 1.0.1】

驗證安裝情況

新建test.py來驗證安裝情況,在test.py文件中輸入以下內容: ``` from koo import * ko()

print(name) 運行結果如下: (pkg) [email protected] pkg % python test.py it is ko invoke mm ko ```

至此已完成wheel從打包到安裝,再到使用得環節。

常規錯誤提示

ERROR:is not support

ERROR: KaKaHelper-1.0.1-py2-none-any.whl is not a supported wheel on this platform. 解答方案: 默認情況下,python環境是2.x,在我當前環境中是python3.x,所以這個安裝不適合我當前環境,另外在安裝的名稱中【py2】也表明了環境,如果需要指定python環境,可以在setup.py文件中使用【python_requires='>=3'】