如何把一个 Python 项目包发布到 PyPI 上指南教程 All In One
如何把一个 Python 项目发布到 PyPI 上指南教程 All In One
How to publish a Python project package on PyPI Tutorial All In One
如何发布一个 Python 项目到 PyPI 上
如何发布一个 Python 包到 PyPI 上
https://pypi.org/user/xgqfrms/
math-package-xgqfrms
✅
https://pypi.org/project/math-package-xgqfrms/
https://github.com/xgqfrms/math_package_project
介绍
PyPI
组织
https://blog.pypi.org/posts/2023-04-23-introducing-pypi-organizations/
官方文档
Packaging Python Projects
打包
Python 项目
https://packaging.python.org/en/latest/tutorials/packaging-projects/
https://packaging.python.org/distributing/
1. 安装工具
pip, 类似 Node.js 生态的 npm 功能
# 更新 pip
$ python3 -m pip install --upgrade pip
https://packaging.python.org/en/latest/key_projects/#pip
pip
is the package installer
for Python.
You can use it to install packages from the Python Package Index
and other indexes.
https://pip.pypa.io/en/stable/
https://pip.pypa.io/en/stable/getting-started/
https://pip.pypa.io/en/stable/installation/
2. 开发项目
项目代码结构树
# tree
$ tree packaging_tutorial
packaging_tutorial/
├── LICENSE
├── pyproject.toml
├── README.md
├── src/
│ └── example_package_YOUR_USERNAME_HERE/
│ ├── __init__.py
│ └── example.py
└── tests/
TODO:
https://github.com/xgqfrms/first_python_package_project
3. 构建项目
Generating distribution archives
生成分发档案
$ python3 -m pip install --upgrade build
$ python3 -m build
4. 发布项目
Uploading the distribution archives
上传分发档案
$ python3 -m pip install --upgrade twine
$ python3 -m twine upload --repository testpypi dist/*
PyPI
API token
&.pypirc
$ cat $HOME/.pypirc
$ vim $HOME/.pypirc
[pypi]
username = your_token_name 复制粘贴到这里
password = your_token 复制粘贴到这里
https://test.pypi.org/help/#apitoken
https://pypi.org/manage/account/token/
5. 安装项目
$ python3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-package-YOUR-USERNAME-HERE
6. 测试项目
# import the package in the REPL
$ python3
> from example_package_YOUR_USERNAME_HERE import example
> example.add_one(2)
3
issues
https://github.com/pypa/packaging-problems
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
demos
.whl
tar.gz 文件是源代码分发,而 .whl 文件是构建的分发
。
较新的 pip 版本优先安装
构建的发行版,但如果需要会回退到源发行版。
prompted
password error ❌
✅
https://test.pypi.org/manage/account/token/
https://test.pypi.org/project/math-package-xgqfrms/0.0.1/
$ pip install -i https://test.pypi.org/simple/ math-package-xgqfrms==0.0.1
https://pypi.org/project/math-package-xgqfrms/0.0.1/
$ python3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps math-package-xgqfrms
from math-package-xgqfrms import math
sum = math.add(1, 2)
print("sum =", sum)
# ✅ namespace.module
from math_package_xgqfrms.math import add, sub, mul, div
n1 = 1
n2 = 2
# sum = math.add(n1, n2)
sum = add(n1, n2)
print("sum =", sum)
# sum = 3
RPi.GPIO 0.7.1
$ pip install RPi.GPIO
https://pypi.org/project/RPi.GPIO/#files
refs
How to use
PyPI
to publish aPython
package All In One
https://www.cnblogs.com/xgqfrms/p/13522683.html
https://github.com/xgqfrms/Python-3.x-All-In-One/tree/main/python-project-package/
https://github.com/xgqfrms/Raspberry-Pi
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17271108.html
未经授权禁止转载,违者必究!