[英]Check if R is installed from python
我有一个调用许多函数的python
代码,这些函数之一需要安装R
软件才能正常运行。
我如何从python
内部检查R
是否已安装在系统中,以避免未调用该函数?
顺便说一句,我正在运行一个Linux发行版(基础操作系统,基于Ubuntu 12.04)
在子进程中使用dpkg -s
:
from subprocess import check_output
print check_output(["dpkg", "-s" , "r-base"])
或者, which
是@kay提示:
from subprocess import Popen, PIPE
proc = Popen(["which", "R"],stdout=PIPE,stderr=PIPE)
exit_code = proc.wait()
if exit_code == 0:
print ("Installed")
使用PIPE
您不会在输出中看到/usr/bin/R
只需测试which R
的结果:
from subprocess import check_call, CalledProcessError
try:
check_call(['which', 'R'])
except CalledProcessError:
print 'Please install R!'
else:
print 'R is installed!'
这也将在* BSD(包括Mac OSX)上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.