python库打包分发1. 准备工作1.1 必备python工具包
1.2 Notes
2. 操作流程2.1 创建setup.py文件——setup.py是包分发与安装的指导文件。 首先,在待打包的库同级目录下创建一个setup.py文件,如下: |--library |--xx.py |--xx.py |--setup.py |--README.md 然后,编写setup.py文件,如下: # from distutils.core import setup from setuptools import setup, find_packages with open("README.md", 'r') as fh: long_description = fh.read() setup( name="library name", # A string specifying the name of the package. version="0.0.1", # A string specifying the version number of the package. author="author name", # A string specifying the author of the package. author_email="author@example.com", # A string specifying the email address of the package author. # maintainer="maintainer name", # A string specifying the name of the current maintainer # maintainer_email="maintainer@example.com", # A string specifying the email address of the current maintainer description="A small example library", # A string describing the package in a single line. long_description=long_description, # A string providing a longer description of the package. long_description_content_type="text/markdown", # A string specifying the content type is used for the long_description url="https://github.com/xxx", # A string specifying the URL for the package homepage. # download_url="xxx.xxx", # A string specifying the URL to download the package. packages=find_packages(), # A list of strings specifying the packages (dictories generally including __init__.py) that setuptools will manipulate. classifiers=[ "Development Status :: 3 - Alpha", # 3 - Alpha, 4 - Beta, 5 - Production/Stable "Topic :: Software Development :: Build Tools", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.7", # "Programming Language :: Python :: 3.8", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], # A list of strings describing the categories for the package. python_requires='>=3', # A string corresponding to a version specifier for the Python version. py_modules=['xx/inference'] # A list of strings specifying the modules that setuptools will manipulate. # install_requires = ['argparse', 'cv2'] # A string or list of strings specifying what other distributions need to be installed when this one is. (即安装此库时,自动会安装install_requires指定的库) ) 更详细的参数说明见keywords。 PS: setuptools.find_packages函数说明: # find_packages函数默认在与 setup.py 文件同一目录下搜索各个含有 __init__.py 的目录做为要添加的包。 find_packages(where='.', exclude=(), include=('*',)) # where指定在哪个目录下搜索包,exclude指定搜索要排除哪些包,include指出搜索要包含的包。 PS: 当然还有另一种编写setup.py文件的方式,即先编写setup.cfg文件,然后通过pbr等解析工具,详见此。 2.2 构建分发包通过setup.py的一些内置命令,即可构建分发包: python setup.py command1 command2 ... 常用commands如下:
所有 py_modules 或 packages 指定的源码文件 所有 ext_modules 指定的文件 所有 package_data 或 data_files 指定的文件 所有 scripts 指定的脚本文件 README、README.txt、setup.py 和 setup.cfg文件
一个构建源码分发包和二进制分发包的小例子,构建好的分发包会自动存储在dist文件夹下: python setup.py sdist bdist_wheel |--build |--xxx |--dist |--{library}-{version}.tar.gz |--{library}-{version}-py{py_version}-none-any.whl |--library |--xx.py |--xx.py |--library.egg-info |--xxx |--setup.py |--README.md 2.3 发布(上传)包到PyPI在构建好分发包后,就可以将分发包上传发布到PyPI中,这样就可以作为一个开源库通过pip install的方式安装使用了。 注意:在上传到PyPI之前,需要在PyPI上注册账号。在实际上传到PyPI前,也可以先尝试上传到TestPyPI做测试,TestPyPI是PyPI的一个单独实例,它允许在不影响实际索引的情况下测试分发工具和进程,TestPyPI一样需要注册账号。注册好后,可以配置一个 . p y p i r c .pypirc .pypirc文件,此文件包含了PyPI访问地址和账号等信息,大致内容如下: [distutils] index-servers = pypi testpypi [pypi] repository = https:/// username:xxx password:xxx [testpypi] repository = https://test./ username:xxx password:xxx 方法一: (1)首先注册项目信息,执行完下方命令之后即可在PyPI中看到项目信息。 python setup.py register (2)上传源码包,之后才可通过pip install下载安装。 python setup.py upload # python setup.py sdist bdist_wheel upload # 同时上传两种分发包 此方法不太推荐。安全性跟可测试性差,只有在构建系统、Python版本和底层操作系统配置都正确的情况下,才能正确且安全地工作。而且,此命令需在构建源码包等命令后最后一步调用,也就是说不能再上传之前测试包。 方法二:(推荐使用) 方法二是通过python的twine库,它是一个专门用于与PyPI交互的工具,其目标就是通过改进安全性和可测试性来改进PyPI交互,其余详细介绍见此。 具体使用(在构建分发包后):
twine upload -r testpypi dist/*
twine upload dist/* 通过简单的一行代码即可将自己的包发布上传到PyPI/TestPyPI中了(执行代码后,会要求操作者输入PyPI/TestPyPI的账户密码进行上传,twine也会自动在home目录下查找.pypirc文件获取账号信息,也可以通过–config-file选项指定文件路径)。 PS: twine check dist/* \text{twine check dist/*} twine check dist/*命令可以检查用户发行版的长描述是否能在PyPI上正确呈现。 参考文献 |
|