网站首页 > 技术文章 正文
Python 项目打包与发布:setuptools 和 wheel
Python 提供了强大的工具链,用于将项目打包并分发到包管理器(如 PyPI)。setuptools 和 wheel 是其中的核心工具。
1. 什么是 setuptools 和 wheel?
- setuptools:用于构建和打包 Python 项目,支持依赖管理和复杂的配置。
- wheel:一种打包格式(.whl),是 Python 包的标准分发形式,安装速度快、效率高。
2. 项目打包的基础结构
以下是一个典型的项目结构:
arduino
Copy code
my_project/
├── my_package/
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
├── tests/
│ ├── test_module1.py
│ └── test_module2.py
├── setup.py
├── README.md
├── LICENSE
└── requirements.txt
- my_package/:核心代码。
- tests/:测试代码。
- setup.py:配置打包信息。
- requirements.txt:列出依赖项。
3. 创建 setup.py
setup.py 是项目打包的配置文件。以下是一个基本示例:
python
Copy code
from setuptools import setup, find_packages
setup(
name="my_project", # 项目名称
version="0.1.0", # 版本号
description="A sample Python project", # 简短描述
long_description=open("README.md").read(), # 详细描述
long_description_content_type="text/markdown", # 描述文件格式
author="Your Name", # 作者
author_email="your_email@example.com", # 作者邮箱
url="https://github.com/your_username/my_project", # 项目主页
license="MIT", # 开源协议
packages=find_packages(), # 自动发现子包
install_requires=[ # 项目依赖
"numpy>=1.21.0",
"requests>=2.26.0"
],
classifiers=[ # 分类信息
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.6", # 支持的 Python 版本
)
4. 构建项目
安装依赖
首先安装打包所需工具:
bash
Copy code
pip install setuptools wheel
构建分发包
在项目根目录运行以下命令:
bash
Copy code
python setup.py sdist bdist_wheel
- sdist:生成源代码分发包,通常是 .tar.gz 文件。
- bdist_wheel:生成 Wheel 分发包(.whl 文件)。
运行后,你将在项目的 dist/ 文件夹中看到生成的包文件。
5. 发布到 PyPI
安装发布工具
bash
Copy code
pip install twine
发布包
将生成的包上传到测试 PyPI(用于测试):
bash
Copy code
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
验证测试包可用后,上传到正式 PyPI:
bash
Copy code
twine upload dist/*
验证发布
安装发布的包:
bash
Copy code
pip install my_project
6. 常见问题与解决方法
1. 没有生成 .whl文件
确保安装了 wheel 工具:
bash
Copy code
pip install wheel
2. 上传失败
检查 PyPI 的用户名和 API 密钥是否正确,或者检查网络连接。
3. 包含静态文件
如果项目中有非代码文件(如配置文件或图片),需要在 setup.py 中配置 include_package_data=True 并添加 MANIFEST.in 文件:
php
Copy code
include README.md
include LICENSE
recursive-include my_package/data *
7. 未来趋势
随着 Python 包管理的发展,以下工具也值得关注:
- poetry:更现代化的依赖管理和打包工具。
- flit:用于简单项目的轻量级打包工具。
通过本文的步骤,您可以快速上手 Python 项目的打包与发布,构建属于自己的 PyPI 包!
猜你喜欢
- 2025-01-24 python时间操作,最全封装,各种年月日加减、转换、获取
- 2025-01-24 [Python办公]Python脚本如何最小化打包成 .exe 文件
- 2025-01-24 将python打包成exe的方式(python程序如何打包生成exe文件)
- 2025-01-24 Python运维常用的20个库(python运维项目)
- 2025-01-24 python 快速打包部署可执行文件(python 快速打包部署可执行文件的方法)
- 2025-01-24 python打包exe与源码保护(python打包程序exe)
- 2025-01-24 python程序打包成.exe执行文件,去掉多余文件,减小体积
- 2025-01-24 python打包exe指南来了,pyinstaller打包教程
- 2025-01-24 python打包exe,各种bug处理,以及解决方案
- 2025-01-24 Python—部署打包工具(python打包配置文件)
- 258℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 83℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)