【发布时间】:2017-01-19 14:27:40
【问题描述】:
我在 Windows 上使用 python。 我想在我的 python 代码中调用一个 R 脚本。
我的脚本 R 是:
c <- 3
print(paste("hello", c))
我的 python 代码像这样调用 Rscript:
import subprocess
subprocess.call(['Rscript', "sb.R"])
很遗憾,我收到此错误消息:
File "C:\Users\[...]\Local\Programs\Python\Python35-32\lib\subproce
ss.py", line 557, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\[...]\Local\Programs\Python\Python35-32\lib\subproce
ss.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Users\[...]\Local\Programs\Python\Python35-32\lib\subproce
ss.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The file is not found
我已经检查了以下内容:
1) 在 powershell 下,当我输入 Rscript .\sb.R 时,它可以工作。
2) 在我尝试过的python代码中
theproc = subprocess.Popen([sys.executable, "sbpy.py"])
theproc.communicate()
其中 sbpy.py 包含 print("hello py")。这也有效。所以我假设库子进程确实有效。
3) 我尝试了以下代码:
cmd = ['Rscript', 'sb.R']
x = subprocess.check_output(cmd, shell=True)
然后我有以下错误:
...
File "C:\Users\[...]\Local\Programs\Python\Python35-32\lib\subproce
ss.py", line 708, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['Rscript', 'sb.R']' returned non-zero e
xit status 1
4) 至于路径,我已经检查过了,是的,R 文件夹在我的路径中。
我在我的windows机器上新安装了python35,是lib子进程中缺少一个文件,还是我错过了什么?
【问题讨论】:
-
在应用程序、系统、当前和
PATH目录中找不到名为“Rscript”或“Rscript.exe”的文件。 -
所以我确实编辑了我的问题。它似乎没有找到 Rscript。我已经检查了路径,它是。此外,在 powershell 中,命令
Rscript .\sb.R确实有效。 -
但这并不意味着 PowerShell 会找到名为“Rscript”或“Rscript.exe”的文件。它还从
PathExt环境变量中查找名称和扩展名。运行where.exe Rscript进行查找。 -
另外,既然您知道 R 的安装位置,请尝试
where.exe /r [R installation path] Rscript递归搜索其文件夹中名为 Rscript、Rscript.exe 等的文件。 -
所以我将整个路径放在命令中,是的,它可以工作。做你所说的意味着在我运行的任何计算机上,我应该将 R 包添加到我的资源然后知道它的路径,或者首先在计算机上找到?那么这是一个有限的解决方案还是只是为了让我的代码正常工作的临时解决方案?
标签: python r windows powershell subprocess