PyPI教程,,Wiki PyPIT


Wiki PyPI

The Python Package Index, abbreviated as PyPI and also known as the Cheese Shop (a reference to the Monty Python‘s Flying Circus sketch Cheese Shop),[1][2] is the official third-party software repository for Python.[3] It is analogous to CPAN, the repository for Perl.[4] Some package managers, including pip, use PyPI as the default source for packages and their dependencies.[5][6] Over 113,000 Python packages can be accessed through PyPI.[7]

就是说PyPI(Python Package Index)是Python官方的第三方库,所有人都可以下载或上传Python库到PyPI。

0x02、流程

1、注册账号

想要上传自己的Python包没PyPI账号怎么行!官网地址

技术图片图片

2、项目架构

1、项目环境:

python3pipsetuptoolswheeltwine

2、项目文件

如果想上传一个名为 "debug_world" 的Python包,那么她的项目文件如下:

-- packaging_tutorial      # 项目名字    -- debug_world         # 包名字        -- __init__.py     # 一定要有init文件        -- print_str.py    # 功能实现    -- __init__.py         # 一定要有init文件    -- README.md           # 一般记录具体使用方法    -- setup.py            # 打包程序
技术图片图片

print_str.py文件实现三个功能,代码如下:

def debug_world():    print(‘世界很美好,我去如此暴躁,这样不好不好‘)def hello_world():    print(‘Hello, world‘)def hello_python():    print(‘Hello Python\nPython 大法好‘)

setup.py文件代码如下:

import setuptoolswith open("README.md", "r") as fh:    long_description = fh.read()setuptools.setup(    name="debug-world",                                     # 包的分发名称,使用字母、数字、_、-    version="0.0.1",                                        # 版本号, 版本号规范:https://www.python.org/dev/peps/pep-0440/    author="liheyou",                                       # 作者名字    author_email="[email protected]",                      # 作者邮箱    description="PyPI Tutorial",                            # 包的简介描述    long_description=long_description,                      # 包的详细介绍(一般通过加载README.md)    long_description_content_type="text/markdown",          # 和上条命令配合使用,声明加载的是markdown文件    url="https://github.com/",                              # 项目开源地址,我这里写的是同性交友官网,大家可以写自己真实的开源网址    packages=setuptools.find_packages(),                    # 如果项目由多个文件组成,我们可以使用find_packages()自动发现所有包和子包,而不是手动列出每个包,在这种情况下,包列表将是example_pkg    classifiers=[                                           # 关于包的其他元数据(metadata)        "Programming Language :: Python :: 3",              # 该软件包仅与Python3兼容        "License :: OSI Approved :: MIT License",           # 根据MIT许可证开源        "Operating System :: OS Independent",               # 与操作系统无关    ],)# 这是最简单的配置# 有关详细信息,请参阅(https://packaging.python.org/guides/distributing-packages-using-setuptools/)

这里是最简单的配置,有关详细信息,请参阅打包和分发项目。

3、本地打包

确保pip,setuptools和wheel是最新的。
虽然pip就能够保证安装成功,但是最新的setuptools和wheel对安装有益无害。

While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:

python -m pip install --upgrade pip setuptools wheel

进入setup.py同级目录,然后运行打包程序

# 运行setup.pypython setup.py sdist# 或者python setup.py sdist bdist_wheel

此时项目会出现两个新文件:


技术图片图片

4、上传PyPI

运行把Python包上传到PyPI的命令

pip install twine     # 如果已经安装twine,跳过次步骤python -m twine upload dist/*# 接着会让你输入PyPI用户名和密码,注意不是邮箱和密码# 出现下面信息说明成功,如有错误信息,检查setup.py配置信息Uploading distributions to https://upload.pypi.org/legacy/Uploading debug-world-0.0.1.tar.gz 100%█████████████████████████| 5.56k/5.56k [00:01<00:00, 3.98kB/s]如果不想每次上传都输入账号和密码,可以创建用户验证文件 ** ~/.pypirc**# 而且此方法不安全,容易泄露密码, 因为密码是明文[distutils]index-servers =    pypi[pypi]repository: https://upload.pypi.org/legacy/username: <username>password: <password>

然后就可以去PyPI官网查看你的包是否成功上传了


技术图片图片

3、验证

PyPI推荐通过pip使用Python包

pip install debug-world

新建验证文件 verify_pypi.py

from debug_world import print_strprint(print_str.hello_world())print(print_str.debug_world())print(print_str.hello_python())

查看运行结果,说明成功了

Hello, world世界很美好,我去如此暴躁,这样不好不好Hello Python    Python 大法好

4、删除版本

当想要删除某一版本的时候,只需在官网项目管理页面进行删除即可。
输入相对应的版本号。


技术图片图片

0x03、注意事项

包名一定是别人没用过的项目文件一定要有** init.py**运行setup.py文件一定要同级目录在上传PyPI的是时候输入的是用户名和密码,不是邮箱和密码上传之后需要等一段时间,才能下载最新版本的包更改包的时候一定要修改版本号pip 按照版本号安装,==前后没有空格

0x04、报错

HTTPError: 400 Client Error: File already exists: 版本号错误
HTTPError: 403 Client Error: Invalid or non-existent authentication information: 密码错误
error: invalid command ‘bdist_wheel‘:

# 升级pip的安装工具setuptoolssudo pip install --upgrade setuptools# 然后更新包python -m pip install --upgrade pip setuptools wheel

0x05、参考

Wiki PyPI
Python Packaging User Guide



作者:DebugWorld
链接:https://www.jianshu.com/p/81fe5a5cd27a
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

PyPI教程

评论关闭