先注册pypi账号
去pypi官网注册账号
创建要打包的模块,目录结构如下:
packaging_tutorial 打包入口 ├── LICENSE 证书 **重要, https://choosealicense.com/ ├── README.md 文档 ├── example_pkg 实际包名,from example_pkg import xxxx └── __init__.py
└── demo01.py 实际代码程序文件
├── setup.py 打包脚本
└── tests 用于单元测试
安装打包模块:
安装setuptools
这个模块, 用来打包python模块, 当然其实还有其他的工具, 这里只介绍这一种, 感兴趣的话可以自行搜索.
pip install --upgrade setuptools
创建LICENSE文件,该文件可有可无
Copyright (c) [year] [The Python Packaging Authority] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
创建README.md文件,该文件可有可无
# Example Package This is a simple example package. You can use [Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/) to write your content.
创建setup.py文件,该文件必须,不是所有字段都是必填,对号入座
"""A setuptools based setup module. See: https://packaging.python.org/en/latest/distributing.html https://github.com/pypa/sampleproject """ # Always prefer setuptools over distutils from setuptools import setup, find_packages # To use a consistent encoding from codecs import open from os import path here = path.abspath(path.dirname(__file__)) # Get the long description from the README file with open(path.join(here, 'README.md'), encoding='utf-8') as f: long_description = f.read() # Arguments marked as "Required" below must be included for upload to PyPI. # Fields marked as "Optional" may be commented out. setup( name='这里是你的模块名字, 和你的module目录名保持一致', version='版本号(每次更新新的版本这里需要递增)', author='作者名', author_email='你的邮箱', description='模块描述', long_description=long_description, # 这里是文档内容, 读取readme文件 long_description_content_type='text/markdown', # 文档格式 packages=find_packages(), classifiers=[ #这里我们指定证书, python版本和系统类型 "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], python_requires='>=3.6', # 这里指定python版本号必须大于3.6才可以安装 install_requires=['pprint', 'tablestore', 'prettytable'] # 我们的模块所用到的依赖, 这里指定的话, 用户安装你的模块时, 会自动安装这些依赖 )
生成distribution archives(可分发归档包),在setup.py文件目录下执行
python setup.py sdist bdist_wheel # 前者是tar.gz文件, 后者是构建.whl文件
该命令执行成功后,会生成三个目录:module.egg-info、build、dist,我们进入到dist目录下会看到生成.tra.gz和.whl后缀的文件
上传归档到PyPI网站
①:使用twine交互式上传,先安装 pip install --upgrade twine twine upload dist/* # 在dist/目录下执行该命令
上传的过程中,会提示输入pypi账号密码,输入后等待上传成功
②:检验是否上传成功
pip install example-pkg
如果不想每次打包输入pypi账号和密码,在当前操作用户目录下面新建文件【.pypirc】文件,文件内容如下:
注:mac进入到/users目录下,再comman+shift+. 查看隐藏文件和目录
[distutils]
index-servers=pypi
[pypi]
repository = https:
//upload.pypi.org/legacy/
username: shenzhiming注册的pypi账号
password: xxx注册的pypi密码