如何使用 distutils api 或 setuptools api 安装 distutils 软件包
我正在编写一个构建脚本,需要在远程服务器上安装 distutils 包。
在 PyPi 上有 2 个方法可以做到这一点 collective.recipe.distutils 0.1 和 Zerokspot.recipe。 distutils 0.1.1。
后一个模块是前一个模块的衍生品,比第一个模块更方便,但是两者都遇到相同的问题,我现在将对此进行描述。
当 bootstrap.py 执行时,它会下载 zc.buildout 包并将其放入 buildout 的 Eggs 目录中。这使 ./bin/buildout 可以访问 zc.buildout 代码,但 /usr/local/python 此时对 zc.buildout 一无所知。
Buildout 尝试通过在子进程内运行“python setup.py install”来安装包。这会产生 ImportError,因为没有为 /usr/local/python 安装 zc.buildout。
所以,我有几个解决方案。
使用 easy_install 在远程服务器上安装 zc.buildout。我根本不喜欢这个选项,它为一个非常微不足道的模块提供了特殊情况。
修改zerokspot.recipe.distutils,以这种方式将try块放在'import zc.buildout'周围,即使没有安装zc.buildout它也会安装。这是一个不错的解决方案,但有点hackish。
将 subprocess 替换为将使用 distutils api 或 setuptools api 安装软件包的代码。在我看来,这将是最好的解决方案。
问题是我该怎么做#3?
谢谢你, Taras
PS:我通过创建另一个不依赖 zc.buildout 的包解决了这个问题。我的包名为 taras.recipe.distutils 并且可以在 pypi 上使用。
I'm working on a buildout script that needs to install a distutils package on remote server.
On PyPi there are 2 recipes for doing this
collective.recipe.distutils 0.1 and zerokspot.recipe.distutils 0.1.1.
The later module a derivative of the former, and is a little more convenient then the first, but the both suffer from the same problem, which I will describe now.
When bootstrap.py is executed, it downloads zc.buildout package and puts it into buildout's eggs directory. This gives ./bin/buildout access to zc.buildout code, but /usr/local/python does not know anything about zc.buildout at this point.
Buildout attepts to install the package by running 'python setup.py install' inside of a subprocess. This produces an ImportError because zc.buildout is not installed for /usr/local/python.
So, I have several solutions.
Install zc.buildout using easy_install on the remote server. I don't like this option at all, it makes a special case for a module that is very insignificant.
Modify zerokspot.recipe.distutils to put try block around 'import zc.buildout' this way, it will install even if zc.buildout is not installed. It's an ok solution, but somewhat hackish.
Replace subprocess with code that will install the package using distutils api or setuptools api. This would be the best solution in my opinion.
The question is how would i do #3?
Thank you,
Taras
PS: I solved the problem by creating another package that does not have dependancy on zc.buildout. My package is called taras.recipe.distutils and it's available on pypi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 子进程 在 Python 程序中调用命令行程序module:
但是,您对此安装运行的环境有多少控制权?如果您要分发的是一个软件包,那么无论人们提出什么解决方案,您都可能会遇到问题。您将如何处理需要 root 访问权限的情况(例如 sudo python setup.py install)?
您可以考虑研究 Paver 因为它提供的 API 在某些方面是扩展设置工具。
You can call a command line program within your Python program using the subprocess module:
However, how much control do you have over the environment that this install will be run? If it is a package that you are distributing, you will likely have issues no matter what solution people propose. How will you handle cases of needing root access (e.g. sudo python setup.py install)?
You may consider looking into Paver since it provides an API that is in some ways an extension of setuptools.
Zerokspot.recipe.distutils 从根本上被破坏了,因为它在 setup.py 中添加了对 zc.buildout 的依赖,如下所示:
setup.py
从导入
get_version
Zerokspot.recipe.distutilszerokspot.recipe.distutils
都在其__init__.py
中定义,包括get_version
导入zerokspot.recipe.distutils
中的 __init__.pyzc.buildout
为什么作者定义
get_version
对我来说是个谜;最佳实践在 setup.py 本身中保留一个简单的版本字符串,并让 setuptools 处理开发版本(通过 setup.cfg),并让 distutils 处理版本元数据提取。通常,在 setup.py 中导入整个包并不是一个好主意,因为这需要在安装时存在所有包依赖项。显然,该软件包的作者已将 zc.buildout 作为站点范围的软件包安装,并且没有注意到他的疏忽。
最好的选择是在 github 上分叉该包,删除 get_version 依赖项,并在使用分叉时向原作者提出更改建议。
zerokspot.recipe.distutils is fundamentally broken in that it adds a dependency on zc.buildout in it's setup.py, as follows:
setup.py
importsget_version
fromzerokspot.recipe.distutils
zerokspot.recipe.distutils
is defined in it's__init__.py
, includingget_version
__init__.py
inzerokspot.recipe.distutils
importszc.buildout
Why the author defines
get_version
is a mystery to me; best practice keeps a simple version string insetup.py
itself and lets setuptools deal with dev versions (throughsetup.cfg
), and distutils for version metadata extraction.Generally it is not a good idea to import the whole package in
setup.py
as that would require all the package dependencies to be present at install time. Obviously the author of the package has zc.buildout installed as a site-wide package and didn't notice his oversight.Your best bet is to fork the package on github, remove the get_version dependency, and propose the change to the original author while you use your fork instead.
您确定不想只生成 bdist 吗?
Are you sure you don't want to just generate a bdist?