我有一个 python
代码,它调用许多函数,其中一个函数需要 R
software安装后才能正常运行。
如何从 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
关于python - 检查 R 是否从 python 安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329955/