我想知道如何在setup.py中使用哪个命令(例如install
或upload
)来运行。
具体来说,我希望:
install
中的特定文件,但没有其他命令。我曾尝试阅读distutils documentation,但在细节方面却非常稀少 - distutils.command [.foo]模块完全没有记录。
对于第一点,我可以检查this question中提到的sys.argv
,但在运行多个命令时不起作用,例如:
python setup.py sdist bdist upload
所以它一般不适用。
答案 0 :(得分:1)
您可以改为覆盖命令:
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
)