2010-12-12 87 views
6

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

+0

相关:[?直接调用的distutils'或setuptools的设置()的命令名称/选项功能,无需解析命令行(http://stackoverflow.com/q/2850971/2127008) – Wrzlprmft 2016-04-24 18:25:14

回答

3
import os 
string = "python setup.py install" 
os.system(string) 
+0

如何要做到这一点,如果setup.py是在其他路径,例如在c:\ foo \ bar \ setup.py? – Aleksandar 2012-12-03 17:02:29

5

可以使用subprocess模块:

import subprocess 
subprocess.call(['python', 'setup.py', 'install']) 
+0

如果setup.py在其他路径上,例如在c:\ foo \ bar \ setup.py中,该怎么做? – Aleksandar 2012-12-03 17:03:38

+2

作为第二个参数传入完整路径。 – sdolan 2012-12-03 18:04:39

0

只是导入。

import setup 
+6

我正在考虑这些方面。一旦导入,我怎么称之为“安装”? – jamesaharvey 2010-12-12 00:39:09

8
+1

我确实阅读过文档。你能给我举个例子吗? 'run_setup('/ home/ubuntu/python-augeas/setup.py',['install'])'不起作用。 – 2017-01-02 14:20:56

+0

@AdamRyczkowski,[DISTUTILS_DEBUG](https://docs.python.org/2/distutils/setupscript.html)是你的朋友 – rstackhouse 2017-09-27 01:52:27

2

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

from setuptools import sandbox 
sandbox.run_setup('setup.py', ['clean', 'bdist_wheel']) 
0

这对我的作品(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. 

找到那么上面的代码应和更改为

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

这么晚了 - 但如果有人在这里发现他/她像我一样 - 这对我有用; (python 3.4)。我的脚本是从setup.py下载的一个包。注意,你必须在setup.py上有chmod + x,我相信。

cwd = os.getcwd() 
parent = os.path.dirname(cwd) 
os.chdir(parent) 
os.system("python setup.py sdist") 
相关问题