如何將Python包發(fā)布到PyPI的完整指南
簡(jiǎn)介
PyPI(Python Package Index)是 Python 官方的第三方軟件倉(cāng)庫(kù),是分發(fā) Python 包的標(biāo)準(zhǔn)平臺(tái)。本文將詳細(xì)介紹如何準(zhǔn)備、打包并發(fā)布Python 項(xiàng)目到 PyPI,并能讓其他開發(fā)者通過(guò) pip install 來(lái)使用您的包。
前期準(zhǔn)備
1. 注冊(cè) PyPI 賬戶
首先,需要在以下兩個(gè)平臺(tái)注冊(cè)賬戶:
- PyPI 正式環(huán)境: https://pypi.org/account/register/
- PyPI 測(cè)試環(huán)境: https://test.pypi.org/account/register/
先在測(cè)試環(huán)境中練習(xí)發(fā)布流程,確保一切正常后再發(fā)布到正式環(huán)境。
2. 安裝必要工具
pip install --upgrade pip pip install --upgrade build pip install --upgrade twine
build: 用于構(gòu)建分發(fā)包twine: 用于上傳包到 PyPI
項(xiàng)目結(jié)構(gòu)準(zhǔn)備
一個(gè)標(biāo)準(zhǔn)的 Python 包應(yīng)該具有以下結(jié)構(gòu):
my_package/ ├── src/ │ └── my_package/ │ ├── __init__.py │ └── module.py ├── tests/ │ └── test_module.py ├── README.md ├── LICENSE ├── pyproject.toml └── setup.py (可選)
關(guān)鍵文件說(shuō)明
1. pyproject.toml (推薦)
這是現(xiàn)代 Python 包的標(biāo)準(zhǔn)配置文件:
[build-system]
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my-awesome-package"
version = "0.1.0"
authors = [
{name = "Your Name", email = "your.email@example.com"},
]
description = "A small example package"
readme = "README.md"
license = {text = "MIT"}
requires-python = ">=3.7"
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
keywords = ["example", "package", "tutorial"]
dependencies = [
"requests>=2.25.0",
"numpy>=1.20.0",
]
[project.optional-dependencies]
dev = [
"pytest>=6.0",
"black",
"flake8",
]
[project.urls]
Homepage = "https://github.com/yourusername/my-awesome-package"
Repository = "https://github.com/yourusername/my-awesome-package"
Documentation = "https://my-awesome-package.readthedocs.io/"
"Bug Tracker" = "https://github.com/yourusername/my-awesome-package/issues"
[project.scripts]
my-command = "my_package.cli:main"
2. setup.py (傳統(tǒng)方式,仍被廣泛支持)
from setuptools import setup, find_packages
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setup(
name="my-awesome-package",
version="0.1.0",
author="Your Name",
author_email="your.email@example.com",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/yourusername/my-awesome-package",
project_urls={
"Bug Tracker": "https://github.com/yourusername/my-awesome-package/issues",
},
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
package_dir={"": "src"},
packages=find_packages(where="src"),
python_requires=">=3.7",
install_requires=[
"requests>=2.25.0",
"numpy>=1.20.0",
],
extras_require={
"dev": [
"pytest>=6.0",
"black",
"flake8",
],
},
entry_points={
"console_scripts": [
"my-command=my_package.cli:main",
],
},
)
3.README.md
編寫清晰的說(shuō)明文檔,包括:
- 項(xiàng)目簡(jiǎn)介
- 安裝方法
- 使用示例
- API 文檔
- 貢獻(xiàn)指南
4.LICENSE
選擇合適的開源許可證,常見的有:
- MIT License(最寬松)
- Apache License 2.0
- GNU GPL v3(最嚴(yán)格)
版本管理
語(yǔ)義化版本控制
遵循 Semantic Versioning 標(biāo)準(zhǔn):
- 主版本號(hào):不兼容的 API 修改
- 次版本號(hào):向下兼容的功能新增
- 修訂版本號(hào):向下兼容的問(wèn)題修正
例如:1.2.3
動(dòng)態(tài)版本管理
可以使用工具自動(dòng)管理版本號(hào):
# src/my_package/__init__.py __version__ = "0.1.0"
# pyproject.toml
[project]
dynamic = ["version"]
[tool.setuptools.dynamic]
version = {attr = "my_package.__version__"}
構(gòu)建和發(fā)布流程
本地測(cè)試
在發(fā)布前,確保包能正確安裝和使用:
# 構(gòu)建包 python -m build # 本地安裝測(cè)試 pip install dist/my_awesome_package-0.1.0-py3-none-any.whl # 測(cè)試導(dǎo)入 python -c "import my_package; print(my_package.__version__)"
發(fā)布到測(cè)試環(huán)境
注冊(cè)Test PyPI賬號(hào)和獲取API密鑰,如下圖所示

安裝發(fā)布工具
pip install twine
檢查包的有效性
twine check dist/*

上傳到測(cè)試PyPI,輸入api密鑰
twine upload --repository testpypi dist/*

發(fā)布到正式環(huán)境
注冊(cè)PyPI賬號(hào)和獲取API密鑰,如下圖所示

上傳包到正式環(huán)境
twine upload dist/*

使用配置文件上傳到Pypi
創(chuàng)建
~/.pypirc文件:[distutils] index-servers = pypi testpypi [pypi] username = __token__ password = pypi-your-api-token-here [testpypi] repository = https://test.pypi.org/legacy/ username = __token__ password = pypi-your-test-api-token-here
上傳到測(cè)試環(huán)境
twine upload --repository testpypi dist/*
上傳到正式環(huán)境
twine upload dist/*
測(cè)試上傳的包
建立一個(gè)新項(xiàng)目,測(cè)試剛剛發(fā)布的包
pip install dlt645

調(diào)用函數(shù),測(cè)試通過(guò)

自動(dòng)化發(fā)布
使用 GitHub Actions
創(chuàng)建 .github/workflows/publish.yml:
name: Publish to PyPI
on:
release:
types: [published]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Build package
run: python -m build
- name: Publish to PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: twine upload dist/*
常見問(wèn)題和解決方案
1. 包名沖突
如果包名已被占用,需要選擇其他名稱??梢裕?/p>
- 添加前綴或后綴
- 使用更具描述性的名稱
2. 上傳失敗
常見原因:
- 版本號(hào)重復(fù)
- 認(rèn)證信息錯(cuò)誤
- 網(wǎng)絡(luò)連接問(wèn)題
3. 安裝后導(dǎo)入失敗
檢查:
- 包結(jié)構(gòu)是否正確
__init__.py文件是否存在- 模塊路徑是否正確
維護(hù)和更新
發(fā)布新版本
- 更新版本號(hào)
- 更新 CHANGELOG
- 構(gòu)建新包
- 上傳到 PyPI
廢棄舊版本
可以在 PyPI 上標(biāo)記特定版本為已廢棄,但不能刪除已發(fā)布的版本。
總結(jié)
到此這篇關(guān)于如何將Python包發(fā)布到PyPI的文章就介紹到這了,更多相關(guān)Python包發(fā)布到PyPI內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中基礎(chǔ)數(shù)據(jù)類型 set集合知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家總結(jié)了一篇關(guān)于Python中基礎(chǔ)數(shù)據(jù)類型 set集合知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-08-08
Python中Dataframe數(shù)據(jù)排序方法(含實(shí)例講解)
在進(jìn)行數(shù)據(jù)分析操作時(shí),經(jīng)常需要對(duì)數(shù)據(jù)按照某行某列排序,或者按照多行多列排序,以及按照索引值排序等等,下面這篇文章主要給大家介紹了關(guān)于Python中Dataframe數(shù)據(jù)排序方法的相關(guān)資料,需要的朋友可以參考下2023-02-02
Python Tkinter Entry和Text的添加與使用詳解
這篇文章主要介紹了Python Tkinter Entry和Text的添加與使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
淺析Python如何實(shí)現(xiàn)Celery任務(wù)隊(duì)列系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了一個(gè)基于 Celery 和 Redis 的分布式任務(wù)隊(duì)列系統(tǒng),用于處理異步任務(wù)和定時(shí)任務(wù),希望對(duì)大家有一定的幫助2025-04-04

