关于Python的setup.py文件

本文通过实例演示了如何使用Python的setup.py文件创建安装包,包括编写setup.py文件、执行sdist命令打包、生成可分发的包以及在不同操作系统上的安装过程。详细解释了setup函数的各项参数及其作用,提供了Windows、Linux环境下生成exe文件和RPM包的方法,并总结了常见参数的功能。

虽然Python的模块可以拷贝安装,但是一般情况下推荐制作一个安装包,即写一个setup.py文件来安装。

如何自己写一个Pythonsetup.py文件呢,我通过一个例子来演示一下。

运行环境

  我的运行环境是centos 6.5Python的版本是2.6.6.

2 操作步骤

  我在centos当前目录下创建了两个文件,一个是setup.py,另外一个是say_helloword.py

   setup.py的文件内容:

from distutils.core import setup
setup(name = "say_helloword" ,
      version = "0.10" ,
      py_modules=['say_helloword'], 
    )
say_helloword.py文件的内容:

def say_hello():
    print 'hello,world!'
然后我在centos当前目录下执行#python setup.py sdist,结果如下图


<img src="https://img-blog.youkuaiyun.com/20140713140415034?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2RrdWl3ZWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
<span style="font-size:18px;"><span style="font-family: 微软雅黑, Arial, Helvetica, sans-serif; line-height: 28.7999992370605px;">在当前目录下,会创建</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; word-wrap: break-word; background: rgb(238, 238, 238);">dist</code><span style="font-family: 微软雅黑, Arial, Helvetica, sans-serif; line-height: 28.7999992370605px;">目录,里面有个文件名为</span></span><p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;"><span style="color: rgb(85, 85, 85); font-family: 宋体; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">say_helloword-0.10.tar.gz</span><span style="line-height: 28.7999992370605px;">,这个就是可以分发的包。使用者拿到这个包后,解压,到</span><span style="font-family: 宋体; line-height: 28.7999992370605px;">say_helloword</span><span style="line-height: 28.7999992370605px;">-1.0目录下执行:</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; word-wrap: break-word; background-image: initial; background-attachment: initial; background-color: rgb(238, 238, 238); background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">python setup.py install</code><span style="line-height: 28.7999992370605px;">,那么,</span><span style="font-family: 宋体; line-height: 28.7999992370605px;">say_helloword</span><span style="line-height: 28.7999992370605px;">.py就会被拷贝到python类路径下,可以被导入使用。</span></p><p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">    <img src="https://img-blog.youkuaiyun.com/20140713140638484?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2RrdWl3ZWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" /></p><p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;"><span style="line-height: 28.7999992370605px;"></span></p><p class="p0" style="margin-bottom:0pt; margin-top:0pt; "><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">对于Windows,可以执行</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">python setup.py bdist_wininst</span><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">生成一个exe文件;若要生成RPM包,执行</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">python setup.py bdist_rpm</span><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">,但系统必须有rpm命令的支持。</span><span style="color: rgb(85, 85, 85); font-size: 9pt; font-family: 宋体; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><o:p></o:p></span></p><p class="p0" style="margin-bottom:0pt; margin-top:0pt; "><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">setup函数还有一些参数:</span><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><o:p></o:p></span></p><span style="white-space:pre">	</span><p class="p0" style="margin-bottom:30.0000pt; margin-top:0pt; text-autospace:ideograph-other; line-height:18.9000pt; "><span style="font-size: 12pt; font-family: 微软雅黑;">1、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">packages</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">告诉Distutils需要处理那些包(包含</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">__init__.py</span><span style="font-size: 12pt; font-family: 微软雅黑;">的文件夹)</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">2、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">package_dir</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">告诉Distutils哪些目录下的文件被映射到哪个源码包。一个例子:</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">package_dir = {'': 'lib'}</span><span style="font-size: 12pt; font-family: 微软雅黑;">,表示“root package”中的模块都在lib目录中。</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">3、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">ext_modules</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">是一个包含Extension实例的列表,Extension的定义也有一些参数。</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">4、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">ext_package</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">定义extension的相对路径</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">5、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">requires</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">定义依赖哪些模块</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">6、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">provides</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">定义可以为哪些模块提供依赖</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">7、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">scripts</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">指定python源码文件,可以从命令行执行。在安装时指定</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">--install-script</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">8、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">package_data</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">通常包含与包实现相关的一些数据文件或类似于readme的文件。如果没有提供模板,会被添加到MANIFEST文件中。</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">9、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">data_files</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">指定其他的一些文件(如配置文件)</span></p><p class="p0" style="margin-bottom:30.0000pt; margin-top:0pt; text-autospace:ideograph-other; line-height:18.9000pt; "><span style="font-size: 16px; line-height: 1.8em; background-color: rgb(255, 255, 255);">规定了哪些文件被安装到哪些目录中。如果目录名是相对路径,则是相对于</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; font-size: 14px; word-wrap: break-word; background: rgb(238, 238, 238);">sys.prefix</code><span style="font-size: 16px; line-height: 1.8em; background-color: rgb(255, 255, 255);">或</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; font-size: 14px; word-wrap: break-word; background: rgb(238, 238, 238);">sys.exec_prefix</code><span style="font-size: 16px; line-height: 1.8em; background-color: rgb(255, 255, 255);">的路径。如果没有提供模板,会被添加到MANIFEST文件中。</span></p>

执行sdist命令时,默认会打包哪些东西呢?

  • 所有由py_modulespackages指定的源码文件
  • 所有由ext_moduleslibraries指定的C源码文件
  • scripts指定的脚本文件
  • 类似于test/test*.py的文件
  • README.txt或README,setup.py,setup.cfg
  • 所有package_datadata_files指定的文件




<think>我们正在处理一个关于在Windows系统上运行Pythonsetup.py文件的问题。根据用户的问题,我们需要提供在Windows上运行setup.py的步骤。 同时,我们需要注意系统级指令:使用正确的LaTeX格式,使用中文回答,生成相关问题,并在引用末尾添加引用标识。 用户的问题:我想在Windows系统上运行Pythonsetup.py文件 请问how to run setup.py file in Windows 根据引用内容,我们知道setup.py是用于打包和分发Python包的文件,通常使用setuptools或distutils。运行setup.py的常见方式是使用命令行。 步骤: 1. 确保已经安装了Python,并且将Python添加到系统环境变量PATH中。 2. 打开命令提示符(CMD)或PowerShell。 3. 切换到setup.py文件所在的目录。 4. 运行命令:`python setup.py <command>`,其中<command>是你要执行的命令,例如install(安装)、build(构建)等。 常见的命令包括: - install: 安装包 - build: 构建包 - sdist: 创建源码分发 - bdist: 创建二进制分发 但是,用户可能只是想安装这个包,所以我们可以重点介绍安装步骤。 另外,引用[4]提到了一种更现代的方式:使用pip来安装,因为pip可以处理依赖。可以使用命令:`pip install -e .`(在setup.py所在目录运行)来以可编辑模式安装。 因此,我们可以提供两种方法。 同时,我们需要注意用户可能遇到的错误,比如引用[1]中提到的错误,可能是由于缺少依赖或环境问题。 回答结构: 1. 介绍运行setup.py的基本方法。 2. 详细步骤(包括两种方法:直接运行setup.py和使用pip)。 3. 可能遇到的问题及解决方法。 生成相关问题:根据用户的问题,我们可以生成一些相关问题,例如如何编写setup.py,或者遇到错误怎么办。 注意:引用标识要自然地添加在引用内容的末尾。 开始回答:</think>### 在 Windows 上运行 Pythonsetup.py 文件 在 Windows 系统上运行 `setup.py` 文件需要以下步骤: 1. **安装 Python 环境** - 从 [Python 官网](https://www.python.org/downloads/windows/) 下载 Windows 版安装包 - 安装时勾选 `Add Python to PATH`(关键步骤) - 验证安装:命令提示符输入 `python --version` 应显示版本号 2. **准备命令行工具** - 按 `Win+R` 输入 `cmd` 打开命令提示符 - 或使用 PowerShell(推荐) 3. **定位到 setup.py 目录** ```shell # 示例(根据实际路径修改): cd C:\Users\YourName\ProjectFolder ``` 4. **执行安装命令** ```shell python setup.py install ``` - 常用命令: - `install`: 安装包到系统 - `build`: 编译扩展模块 - `develop`: 创建开发模式链接(引用[4]) - `sdist`: 生成源码分发包 5. **替代方法(推荐)** ```shell pip install -e . # 可编辑模式安装(引用[4]) ``` **常见问题解决:** - **权限错误**:以管理员身份运行命令提示符 - **依赖缺失**:先执行 `pip install -r requirements.txt` - **编译错误**:安装 [Microsoft C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) - **路径问题**:确保在包含 `setup.py` 的目录执行命令(引用[1]) > 注意:现代 Python 项目推荐使用 `pip` 和 `pyproject.toml` 替代直接执行 `setup.py`(引用[2])。对于包含 Cython 扩展的项目,需预先配置编译环境(引用[3])。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值