如何在Python中运行'python setup.py install'?
我正在尝试创建一个通用的Python脚本,用来启动一个Python应用程序。如果目标系统上缺少任何依赖的Python模块,我希望能够自动安装它们。我想知道如何在Python代码中运行类似于命令行中'python setup.py install'的命令。感觉这应该很简单,但我就是搞不定。
8 个回答
6
这对我来说有效(py2.7)
我有一个可选的模块,它的setup.py文件放在主项目的一个子文件夹里。
from distutils.core import run_setup
[..主项目的setup(..)配置..]
run_setup('subfolder/setup.py', script_args=['develop',],stop_after='run')
谢谢
更新:
经过一段时间的研究,你可以在distutils.core.run_setup中找到
'script_name' is a file that will be run with 'execfile()'; 'sys.argv[0]' will be replaced with 'script' for the duration of the call. 'script_args' is a list of strings; if supplied, 'sys.argv[1:]' will be replaced by 'script_args' for the duration of the call.
所以上面的代码应该改成
import sys
from distutils.core import run_setup
run_setup('subfolder/setup.py', script_args=sys.argv[1:],stop_after='run')
6
你可以使用 subprocess 这个模块:
import subprocess
subprocess.call(['python', 'setup.py', 'install'])
13
对于使用setuptools的人来说,你可以使用setuptools.sandbox:
from setuptools import sandbox
sandbox.run_setup('setup.py', ['clean', 'bdist_wheel'])