python的setup.py文件及其常用命令

本文介绍了Python项目中的setup.py文件及其各种命令的用途,包括构建、安装、打包等操作,并提供了命令示例及如何通过setup.py文件自定义命令。
python的setup.py文件及其常用命令
2012-09-07 08:53:44      我来说两句       作者:cjh6311882
收藏   我要投稿

编写setup.py文件,获取帮助:python setup.py --help-commands

[python] 
Standard commands: 
  build             build everything needed to install 
  build_py          "build" pure Python modules (copy to build directory) 
  build_ext         build C/C++ extensions (compile/link to build directory) 
  build_clib        build C/C++ libraries used by Python extensions 
  build_scripts     "build" scripts (copy and fixup #! line) 
  clean             clean up temporary files from 'build' command 
  install           install everything from build directory 
  install_lib       install all Python modules (extensions and pure Python) 
  install_headers   install C/C++ header files 
  install_scripts   install scripts (Python or otherwise) 
  install_data      install data files 
  sdist             create a source distribution (tarball, zip file, etc.) 
  register          register the distribution with the Python package index 
  bdist             create a built (binary) distribution 
  bdist_dumb        create a "dumb" built distribution 
  bdist_rpm         create an RPM distribution 
  bdist_wininst     create an executable installer for MS Windows 
  upload            upload binary package to PyPI 
  check             perform some checks on the package 
 
Extra commands: 
  rotate            delete older distributions, keeping N newest files 
  develop           install package in 'development mode' 
  setopt            set an option in setup.cfg or another config file 
  saveopts          save supplied options to setup.cfg or other config file 
  egg_info          create a distribution's .egg-info directory 
  install_egg_info  Install an .egg-info directory for the package 
  alias             define a shortcut to invoke one or more commands 
  easy_install      Find/get/install Python packages 
  bdist_egg         create an "egg" distribution 
  test              run unit tests after in-place build 

 

standard commands(标准命令)是distutils内建命令,而Extra commands(附加命令)是像

setuptools这样的第三方包创建的。

命令详解:
1.sdist:用来创建一个源码包,在windows下为zip格式,linux下为tag.gz格式 。
setup.py示例:
from setuptools import setup
setup(name='xxx',version='0.1.1')
打包命令:python setup.py sdist
distutils将浏览包的路径,查找包含在档案中的文件,包括:
   1)所有py_modules、packages和scripts选项隐含的python源文件
   2)所有在ext_modules选项中列出的C源文件
   3)符合test/test*.py模式的文件
   4)README、README.txt、setup.py和setup.cfg文件


如果需要包含更多文件,那么可以使用MANIFEST.in文件模版,示例如下:
include xx.txt
include yy/xx.txt
....
2.bdist_egg:用来创建一个二进制分发版本,经常用来替代基于bdist生成的模式


3.install:安装包到python中。另外,他还可能会安装在setup.py中的install_requires定义的

相关模块,例如:

   install_requires=['...','...']

删除一个包:1)删除这个包所创建的文件,2)删除在site-packages文件夹easy-install.pth文

件中列举的所有引用。 www.2cto.com


4.develop:编译并且在适当的位置安装包,然后添加一个简单的链接到python site-packages文

件夹中,可以使用显式的-u选项删除包,例如:
 python setup.py develop
 python setup.py develop -u
    使用该方式比其他方式安装包更好一些

 

5.创建一个新命令:这是由setuptools引入的,是一种将包定义为插件的简单方法,示例:
...entry_points='''
                [distutils.commands]
  my_command=my.command.module.Class

<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])。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值