如何在Python中运行'python setup.py install'?

jam*_*vey 10 python

我正在尝试创建一个通用的python脚本来启动一个python应用程序,我想安装任何依赖的python模块,如果它们从目标系统中丢失.如何从Python本身运行命令行命令'python setup.py install'的等效命令?我觉得这应该很容易,但我无法弄清楚.

Bar*_*ski 8

对于那些使用setuptools的用户,可以使用setuptools.sandbox

from setuptools import sandbox
sandbox.run_setup('setup.py', ['clean', 'bdist_wheel'])
Run Code Online (Sandbox Code Playgroud)


Lit*_*ter 6

这对我有用(py2.7)
我在主项目的子文件夹中有一个带有 setup.py 的可选模块。

from distutils.core import run_setup [..setup(..) config of the main project..] 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.
Run Code Online (Sandbox Code Playgroud)

所以上面的代码应该改为

import sys
from distutils.core import run_setup
run_setup('subfolder/setup.py', script_args=sys.argv[1:],stop_after='run')
Run Code Online (Sandbox Code Playgroud)


Sam*_*lan 5

您可以使用子进程模块:

import subprocess
subprocess.call(['python', 'setup.py', 'install'])
Run Code Online (Sandbox Code Playgroud)

  • 作为第二个参数传递完整路径. (2认同)

dls*_*dls 2

import os
string = "python setup.py install"
os.system(string)
Run Code Online (Sandbox Code Playgroud)