Python setup.py 构建工具参数详解与配置实战

雨后青山
雨后青山
发布时间:2026-10-12| 4人浏览过
原创

Python 项目的构建与分发通常依赖于 setup.py 文件。与 pip install 自动处理在线依赖不同,python setup.py install 属于本地源码安装,不会自动拉取依赖包。本文通过具体代码示例,解析如何利用 setup.py 解决依赖包与自定义模块的打包发布问题,包括生成全局可执行文件及配置命令行入口。

setup.py 核心参数解析

setup.py 是 Python 项目的构建脚本,通过 setuptools.setup() 函数定义项目元数据。以下是常用参数的分类说明:

  • 基础信息:name 指定包名称;version 或 -V 指定版本号;author 与 author_email 定义作者及邮箱;maintainer 与 maintainer_email 指定维护者信息。
  • 项目描述:url 指向官网;license 声明授权协议;description 为简短描述;long_description 提供详细文档。
  • 分类与平台:platforms 指定适用平台列表;classifiers 定义 PyPI 分类标签;keywords 设置关键字列表。
  • 包与模块:packages 指定包含 __init__.py 的包目录,通常配合 find_packages() 使用;py_modules 指定需打包的独立 Python 文件列表。
  • 数据与脚本:data_files 打包非代码文件(如图片、配置);scripts 指定安装时执行的脚本列表。
  • 依赖管理:install_requires 定义运行时依赖(如 requests);requires 定义前置依赖;provides 声明当前模块提供的依赖。

基础配置示例

以下是一个基础的 setup.py 配置,展示了如何定义包名、版本、依赖及入口点:

from setuptools import setup, find_packages 
 
setup( 
 name = "test", 
 version = "1.0", 
 keywords = ("test", "xxx"), 
 description = "eds sdk", 
 long_description = "eds sdk for python", 
 license = "MIT Licence", 
 url = "http://test.com", 
 author = "test", 
 author_email = "test@gmail.com", 
 packages = find_packages(), 
 include_package_data = True, 
 platforms = "any", 
 install_requires = [], 
 scripts = [], 
 entry_points = { 
  'console_scripts': [ 
   'test = test.help:main' 
  ] 
 } 
)

排除特定包

若需排除测试目录,可使用 find_packages() 的 exclude 参数:

find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"])

命令行工具配置 (entry_points)

通过 entry_points 中的 console_scripts,可以将 Python 脚本打包为命令行工具。等号前为工具名称,等号后为模块路径与入口函数。

单入口配置

entry_points={'console_scripts': [
   'redis_run = RedisRun.redis_run:main',
 ]}

多入口配置

支持定义多个命令行工具:

setup(
 entry_points = {
  'console_scripts': [
   'foo = demo:test',
   'bar = demo:test',
  ]}
)

完整实战示例:打包 Redis 服务

以下示例演示如何将 Redis 订阅服务打包,并在 Windows 环境下生成 exe 文件至 Scripts 目录。请注意,模块与函数之间需使用冒号 : 分隔。

#!/usr/bin/env python
# coding=utf-8

from setuptools import setup

'''
把redis服务打包成C:\Python27\Scripts下的exe文件
'''

setup(
 name="RedisRun", #pypi中的名称,pip或者easy_install安装时使用的名称,或生成egg文件的名称
 version="1.0",
 author="Andreas Schroeder",
 author_email="andreas@drqueue.org",
 description=("This is a service of redis subscripe"),
 license="GPLv3",
 keywords="redis subscripe",
 url="https://ssl.xxx.org/redmine/projects/RedisRun",
 packages=['RedisRun'], # 需要打包的目录列表

 # 需要安装的依赖
 install_requires=[
  'redis>=2.10.5',
  'setuptools>=16.0',
 ],

 # 添加这个选项,在windows下Python目录的scripts下生成exe文件
 # 注意:模块与函数之间是冒号:
 entry_points={'console_scripts': [
  'redis_run = RedisRun.redis_run:main',
 ]},

 # long_description=read('README.md'),
 classifiers=[ # 程序的所属分类列表
  "Development Status :: 3 - Alpha",
  "Topic :: Utilities",
  "License :: OSI Approved :: GNU General Public License (GPL)",
 ],
 # 此项需要,否则卸载时报windows error
 zip_safe=False
)

另一个变体示例,修改了包名与入口路径:

#!/usr/bin/env python
# coding=utf-8

from setuptools import setup

'''
把redis服务打包成C:\Python27\Scripts下的exe文件
'''

setup(
 name="RedisRun", #pypi中的名称,pip或者easy_install安装时使用的名称
 version="1.0",
 author="Andreas Schroeder",
 author_email="andreas@drqueue.org",
 description=("This is a service of redis subscripe"),
 license="GPLv3",
 keywords="redis subscripe",
 url="https://ssl.xxx.org/redmine/projects/RedisRun",
 packages=['DrQueue'], # 需要打包的目录列表

 # 需要安装的依赖
 install_requires=[
  'redis>=2.10.5',
 ],

 # 添加这个选项,在windows下Python目录的scripts下生成exe文件
 # 注意:模块与函数之间是冒号:
 entry_points={'console_scripts': [
  'redis_run = DrQueue.RedisRun.redis_run:main',
 ]},

 # long_description=read('README.md'),
 classifiers=[ # 程序的所属分类列表
  "Development Status :: 3 - Alpha",
  "Topic :: Utilities",
  "License :: OSI Approved :: GNU General Public License (GPL)",
 ],
 # 此项需要,否则卸载时报windows error
 zip_safe=False
)

总结

通过合理配置 setup.py,开发者可以灵活控制 Python 项目的打包行为,包括依赖管理、平台适配及命令行工具生成。掌握这些参数有助于提高项目发布的标准化程度与可维护性。

以上就是 Python 构建工具 setup.py 方法使用的详细内容,更多关于 Python 打包与发布的技术资料请关注本站其它相关文章!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。