[英]Running R script with subprocess on Windows, subprocess seems missing
我在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子进程中缺少的文件,还是我错过了什么?
检查你的python脚本和Rscript是否是相同的文件位置。
示例:在桌面位置下创建名为sb.R和subprocess_demo.py的文件。 在命令提示符下将目录更改为Desktop并运行以下命令python subprocess_demo.py
sb.R
c <- 3
print(paste("hello", c))
subprocess_demo.py
import subprocess
subprocess.call(['Rscript', "sb.R"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.