python 如何使用简单的安装后脚本扩展 distutils?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1321270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extend distutils with a simple post install script?
提问by blokkie
I need to run a simple script after the modules and programs have been installed. I'm having a little trouble finding straight-forward documentation on how to do this. It looks like I need to inherit from distutils.command.install, override some methods and add this object to the setup script. The specifics are a bit hazy though and it seems like a lot of effort for such a simple hook. Does anyone know an easy way to do this?
在安装模块和程序后,我需要运行一个简单的脚本。我在查找有关如何执行此操作的直接文档时遇到了一些麻烦。看起来我需要从 distutils.command.install 继承,覆盖一些方法并将这个对象添加到安装脚本中。虽然细节有点模糊,但对于这样一个简单的钩子来说似乎需要付出很多努力。有谁知道一个简单的方法来做到这一点?
采纳答案by blokkie
I dug through distutils source for a day to learn enough about it to make a bunch of custom commands. It's not pretty, but it does work.
我挖了一天的 distutils 源来了解它来制作一堆自定义命令。它不漂亮,但确实有效。
import distutils.core
from distutils.command.install import install
...
class my_install(install):
def run(self):
install.run(self)
# Custom stuff here
# distutils.command.install actually has some nice helper methods
# and interfaces. I strongly suggest reading the docstrings.
...
distutils.core.setup(..., cmdclass=dict(install=my_install), ...)
回答by blokkie
OK, I figured it out. The idea is basically to extend one of the distutils commands and overwrite the run method. To tell distutils to use the new class you can use the cmdclass variable.
好的,我想通了。这个想法基本上是扩展 distutils 命令之一并覆盖 run 方法。要告诉 distutils 使用新类,您可以使用 cmdclass 变量。
from distutils.core import setup
from distutils.command.install_data import install_data
class post_install(install_data):
def run(self):
# Call parent
install_data.run(self)
# Execute commands
print "Running"
setup(name="example",
cmdclass={"install_data": post_install},
...
)
Hope this will help someone else.
希望这会帮助别人。
回答by Woltan
I couldn't make Joe Wreschnigs answer work and tweaked his answer analogous to the extending distutils documentation. I came up with this code which works fine on my machine.
我无法让Joe Wreschnig的答案起作用,并类似于扩展的 distutils文档调整了他的答案。我想出了这段代码,它在我的机器上运行良好。
from distutils import core
from distutils.command.install import install
...
class my_install(install):
def run(self):
install.run(self)
# Custom stuff here
# distutils.command.install actually has some nice helper methods
# and interfaces. I strongly suggest reading the docstrings.
...
distutils.core.setup(..., cmdclass={'install': my_install})
Note: I didn't edit the answer of Joe since I am uncertain why his answer wasn't working on my machine.
注意:我没有编辑 Joe 的答案,因为我不确定为什么他的答案在我的机器上不起作用。
回答by Theuns Alberts
I got an error when I tried the accepted answer here (might be because I'm using Python 2.6 in this particular case, not sure). This occurred for both 'setup.py install' and 'pip install':
当我在这里尝试接受的答案时出现错误(可能是因为在这种特殊情况下我使用的是 Python 2.6,不确定)。'setup.py install' 和 'pip install' 都会发生这种情况:
sudo python setup.py install
fails with error: error in setup.cfg: command 'my_install' has no such option 'single_version_externally_managed'
失败并出现错误:setup.cfg 中的错误:命令“my_install”没有这样的选项“single_version_externally_managed”
AND
和
sudo pip install . -U
fails more verbosely but also with error: option --single-version-externally-managed not recognized
失败更详细,但也有错误:选项 --single-version-externally-managed 无法识别
Variation on the accepted answer
已接受答案的变化
Replacing the imports from distutilswith setuptoolssolved the problem for me:
用setuptools替换来自distutils的导入为我解决了这个问题:
from setuptools import setup
from setuptools.command.install import install