【Python】应用:发布pyproject.toml格式包到 PyPI

😏★,° :.☆( ̄▽ ̄)/$:.°★ 😏

这篇文章主要介绍发布pip包到PyPI官方仓库。
无专精则不能成,无涉猎则不能通。------梁启超

欢迎来到我的博客,一起学习,共同进步。

喜欢的朋友可以关注一下,下次更新不迷路🥞

文章目录

    • [:smirk:1. 项目准备](#:smirk:1. 项目准备)
    • [:blush:2. 安装发布工具](#:blush:2. 安装发布工具)
    • [:satisfied:3. 上传到PyPI](#:satisfied:3. 上传到PyPI)

😏1. 项目准备

首先,推荐一下我自己打包分发的 pip 包!!!(IP多功能工具

可以通过pip安装:pip install ipdisp

bash 复制代码
https://pypi.org/project/ipdisp/#description
  1. 准备项目/包结构
sh 复制代码
  my-package/
  ├── pyproject.toml
  ├── src/
  │   └── my_package/
  │       ├── __init__.py
  │       └── main.py
  ├── README.md
  └── LICENSE
  1. 编写 pyproject.toml
cpp 复制代码
 [build-system]
  requires = ["setuptools>=61.0", "wheel"]
  build-backend = "setuptools.build_meta"

  [project]
  name = "my-package"
  version = "0.1.0"
  description = "A short description"
  readme = "README.md"
  license = {text = "MIT"}
  requires-python = ">=3.10"
  authors = [
      {name = "Your Name", email = "you@example.com"}
  ]
  classifiers = [
      "Programming Language :: Python :: 3",
  ]
  dependencies = [
      "requests>=2.28",
  ]

  [project.optional-dependencies]
  dev = ["pytest", "black", "mypy"]
  1. 编写 src/my_package/init.py

version = "0.1.0"

😊2. 安装发布工具

  1. 注册 PyPI 账号

PyPI 生产环境

cpp 复制代码
 https://pypi.org/account/register/

Test PyPI (测试用)

cpp 复制代码
 https://test.pypi.org/account/register/
  1. 安装发布工具
cpp 复制代码
pip install build twine
  1. 构建源码和 wheel
cpp 复制代码
python -m build

会在 dist/ 目录下生成:

bash 复制代码
  dist/
  ├── my_package-0.1.0.tar.gz      # 源码
  └── my_package-0.1.1-py3-none-any.whl  # wheel

😆3. 上传到PyPI

  1. 上传到 PyPI

方式1: twine (推荐,更安全)

bash 复制代码
twine upload dist/*

按提示输入 username / password

方式2: 直接用 build

bash 复制代码
python -m twine upload dist/*
  1. 配置认证信息(可选)

在 ~/.pypirc 或用环境变量,避免每次输入密码:

bash 复制代码
 # 环境变量方式
  export PYPI_USERNAME="__token__"
  export PYPI_PASSWORD="pypi-xxxx_your_token_xxxx"

在 PyPI → Account → API Tokens,生成一个 token,username 填 token,password 填 token 值。

常见问题:

可以发布前先用 Test PyPI 验证:

bash 复制代码
  twine upload --repository testpypi dist/*
  pip install --index-url https://test.pypi.org/simple/ my-package

确认没问题再上正式 PyPI。

以上。

相关推荐
小小测试开发6 小时前
安装 Python 3.10+
开发语言·人工智能·python
梦想不只是梦与想7 小时前
Python 中的装饰器
python·装饰器
我叫唧唧波7 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
AAA大运重卡何师傅(专跑国道)8 小时前
【无标题】
开发语言·c#
copyer_xyf8 小时前
Python 异常处理
前端·后端·python
XBodhi.8 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
麻雀飞吧8 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy9 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.9 小时前
【01】Python 机器学习
开发语言·python
为爱停留9 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python