用Python将库打包发布到pypi
如果需要将自己写好的python打包,并发布到pypi,这样其他人就可以直接通过pip install
来安装对应的包,可以参考如下教程
1. 注册pypi账号并创建token
首先访问https://pypi.org/ 并注册账号
然后跳转到账号设置
然后选择api token->add api token
输入token name并在scope中选择entire account(第一次需要选择entire account)
然后在本地,修改.pypirc
文件
输入的内容为:
[pypi] username = __token__ password = {token}
只需要修改{token}
为自己的token即可
2. 编写setup.py和setup.cfg
setup.cfg的内容为
[metadata] license_files = license.txt
license.txt是license文件,需要自行编写
setup.py在根目录下,一个示例为
from setuptools import setup import compileall from os import path # 读取readme文件,这样可以直接显示在主页上 this_directory = path.abspath(path.dirname(__file__)) with open(path.join(this_directory, 'readme.md'), encoding='utf-8') as f: long_description = f.read() compileall.compile_dir("src") setup( name='my-python', version='1.0.2', packages=['src', 'src.main', 'src.main.config'], url='https://github.com/htangle', license='apache 2.0', author='htangle', author_email='', description='', keywords='', python_requires='>=3.4, <4', long_description=long_description, long_description_content_type='text/markdown', install_requires=['requests'] )
具体的字段含义如下:
name
: 包名
version
: 版本号,支持如下形式
1.2.0.dev1 # development release 1.2.0a1 # alpha release 1.2.0b1 # beta release 1.2.0rc1 # release candidate 1.2.0 # final release 1.2.0.post1 # post release 15.10 # date based release 23 # serial release
description
: 包描述,会放在如下图所示的位置处
url
: 包的链接,可以使用github链接,pypi会自动获取到仓库的信息,示例如下:
author
: 作者
license
: 许可证
classifiers
: 分类,示例如下:
classifiers=[ # how mature is this project? common values are # 3 - alpha # 4 - beta # 5 - production/stable 'development status :: 3 - alpha', # indicate who your project is intended for 'intended audience :: developers', 'topic :: software development :: build tools', # pick your license as you wish (should match "license" above) 'license :: osi approved :: mit license', # specify the python versions you support here. in particular, ensure # that you indicate whether you support python 2, python 3 or both. 'programming language :: python :: 2', 'programming language :: python :: 2.7', 'programming language :: python :: 3', 'programming language :: python :: 3.6', 'programming language :: python :: 3.7', 'programming language :: python :: 3.8', 'programming language :: python :: 3.9', ],
keywords
: 关键字,和论文的关键字类似
project_urls
: 一些项目的其他链接,示例如下
project_urls={ 'documentation': 'https://packaging.python.org/tutorials/distributing-packages/', 'funding': 'https://donate.pypi.org', 'say thanks!': 'http://saythanks.io/to/example', 'source': 'https://github.com/pypa/sampleproject/', 'tracker': 'https://github.com/pypa/sampleproject/issues', },
packages
: 需要打包的目录,需要以根目录为起点,可以使用
find_packages
自动查找包,注意不要漏写
install_requires
: 包依赖的其他包
python_requires
: python的版本需求
package_data
: 需要的额外的文件,例如包强依赖一个本地文件,可以使用如下
package_data={ 'sample': ['package_data.dat'], },
3. 打包
打包命令为
python setup.py cmd
cmd可以取值为
bdist_wheel : create a wheel distribution
bdist_egg : create an “egg” distribution
sdist : create a source distribution (tarball, zip file, etc.)
bdist : create a built (binary) distribution
bdist_dumb : create a “dumb” built distribution
bdist_rpm : create an rpm distribution
bdist_wininst : create an executable installer for ms windows
打包为tar.gz
python setup.py sdist
打包好的文件再dist目录下
4. 上传
可以首先使用twine
对包进行检查
twine check dist/*
输出如下
再运行上传命令
twine upload dist/*
到此这篇关于用python将库打包发布到pypi的文章就介绍到这了,更多相关python打包到pypi内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!