编程语言
首页 > 编程语言> > python的神奇功能二、源码打包

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

作者:互联网

「这是我参与2022首次更文挑战的第7天,活动详情查看:2022首次更文挑战

源码打包~wheel方式

python的wheel介绍

wheel是python中的一个打包方式,它可以为你软件的依赖库或依赖软件提供打包,它打包的依赖包可以在每次使用都可以很方便的安装而不需要再次编译。

wheel的官方文档是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官方文档setuptools.readthedocs.io/en/latest/.

wheel命令的相关帮助信息

(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

python 的wheel打包方式

整理源码为python模块

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

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

name = 'mm ko'

from setuptools import setup

setup(name='KaKaHelper',version='1.0.1',py_modules=['koo'],description='A helper for KaKa',author='KaKa',author_email='jjkchen@foxmail.com',license='MIT',keywords='ka helper',install_requires=['scapy>=2.4.4','psutil>=5.8.0'],python_requires='>=3'),

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文件中。

# KaKaHelper
This is helper for kaka

在__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) michaelkoo@MacBook kaka % ls dist 
KaKaHelper-1.0.1.tar.gz

截止到目前为止,以【源代码发布包】的形式已打包完成。

 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安装包:

michaelkoo@MacBook kaka % ls dist 
KaKaHelper-1.0.1-py3-none-any.whl	KaKaHelper-1.0.1.tar.gz

python 的wheel安装方式

按照以上步骤,我们生成好了wheel安装包,那我们现在来安装:

(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

从以上输出可知,当前环境中没有安装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) 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

可以看到已经安装好【KaKaHelper 1.0.1】

验证安装情况

新建test.py来验证安装情况,在test.py文件中输入以下内容:

from koo import *
ko()

print(name)

运行结果如下:

(pkg) michaelkoo@MacBook 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'】

标签:info,wheel,1.0,python,KaKaHelper,py,源码,egg,打包
来源: https://blog.csdn.net/m0_59235508/article/details/122716047