Python 如何检查setup.py中运行的命令?
我想知道如何使setup.py中的一些代码以运行哪个命令(例如Python 如何检查setup.py中运行的命令?,python,distutils,setup.py,Python,Distutils,Setup.py,我想知道如何使setup.py中的一些代码以运行哪个命令(例如install或upload)为条件 具体而言,我希望: 添加“黑客”的简单方法,例如忽略安装中的特定文件,但不使用其他命令 一种推荐的/规范的方式来添加钩子,例如在安装之前运行测试 我已经试着阅读了,但是在细节上却很少——distutils.command[.foo]模块完全没有文档记录 对于第一点,我可以检查中提到的sys.argv,但当运行多个命令时,这不起作用,例如: python setup.py sdist bdist u
install
或upload
)为条件
具体而言,我希望:
安装中的特定文件,但不使用其他命令
sys.argv
,但当运行多个命令时,这不起作用,例如:
python setup.py sdist bdist upload
因此它一般不适用。您可以替代该命令:
from distutils.command.install import install
from distutils.core import setup
def run_file(path):
with open(path, 'r') as f:
exec(f.read())
class myinstall(install): # subclass distutils's install command
def finalize_options(self): # called after option parsing
# call base class function
install.finalize_options(self)
# super won't work because distutils under Python 2 uses old-style classes
# ignore a module
self.distribution.py_modules.remove('mymodule')
def run(self): # called to run a command
# run tests first
run_file('path/to/test.py')
# ^ remember to make sure the module is in sys.path
# run the real commands
install.run(self)
setup(
name='abc',
py_modules=['mymodule'],
cmdclass={'install': myinstall}
# ^ override the install command
)
正是我想要的。相同的代码在2和3中可以工作吗?@otus:现在可以了。:)原来他们删除了Python3中的execfile,所以您必须推出自己的版本。