标签:介绍 you 配置信息 setup selector ima fun 世界 有关详细信息
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。
想要上传自己的Python包没PyPI账号怎么行!官网地址
如果想上传一个名为 "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 setuptools
with 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="author@example.com", # 作者邮箱
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/)
这里是最简单的配置,有关详细信息,请参阅打包和分发项目。
确保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.py
python setup.py sdist
# 或者
python setup.py sdist bdist_wheel
此时项目会出现两个新文件:
运行把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官网查看你的包是否成功上传了
PyPI推荐通过pip使用Python包
pip install debug-world
新建验证文件 verify_pypi.py
from debug_world import print_str
print(print_str.hello_world())
print(print_str.debug_world())
print(print_str.hello_python())
查看运行结果,说明成功了
Hello, world
世界很美好,我去如此暴躁,这样不好不好
Hello Python Python 大法好
当想要删除某一版本的时候,只需在官网项目管理页面进行删除即可。
输入相对应的版本号。
HTTPError: 400 Client Error: File already exists: 版本号错误
HTTPError: 403 Client Error: Invalid or non-existent authentication information: 密码错误
error: invalid command ‘bdist_wheel‘:
# 升级pip的安装工具setuptools
sudo pip install --upgrade setuptools
# 然后更新包
python -m pip install --upgrade pip setuptools wheel
标签:介绍 you 配置信息 setup selector ima fun 世界 有关详细信息
原文地址:https://www.cnblogs.com/wisir/p/11192252.html