Python 包发布到 PyPI 后,用户可通过
pip install直接安装。
项目结构
my_package/
├── my_package/
│ ├── __init__.py
│ └── core.py
├── setup.py
├── pyproject.toml
└── README.mdsetup.py 示例
from setuptools import setup, find_packages
setup(
name="my_package",
version="0.1.0",
packages=find_packages(),
install_requires=[
"requests>=2.25.0",
],
author="Your Name",
author_email="you@example.com",
description="A short description",
url="https://github.com/username/my_package",
)发布步骤
# 安装构建工具
pip install build twine
# 构建
python -m build
# 上传到 PyPI
twine upload dist/*配置 PyPI Token
- 在 PyPI 创建 API Token
- 创建
~/.pypirc:
[pypi]
username = __token__
password = pypi-xxx...