简体   繁体   English

在Windows上使用子进程运行R脚本,似乎缺少子进程

[英]Running R script with subprocess on Windows, subprocess seems missing

I am using python on windows. 我在Windows上使用python。 I want to call an R script in my python code. 我想在我的python代码中调用R脚本。

My script R is : 我的脚本R是:

c <- 3
print(paste("hello", c))

My python code calls the Rscript like this: 我的python代码调用Rscript,如下所示:

import subprocess
subprocess.call(['Rscript', "sb.R"])

Unfortunately I have this error message : 不幸的是我有这个错误消息:

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

I have checked the following: 我检查了以下内容:

1) under powershell, when I enter Rscript .\\sb.R it works. 1)在powershell下,当我输入Rscript .\\sb.R它有效。

2) in the python code I tried 2)在我试过的python代码中

theproc = subprocess.Popen([sys.executable, "sbpy.py"])
theproc.communicate()

where sbpy.py contains print("hello py") . 其中sbpy.py包含print("hello py") This works as well. 这也有效。 So I assume that the library subprocess do work. 所以我假设库子进程确实有效。

3) I tried the following code : 3)我尝试了以下代码:

cmd = ['Rscript', 'sb.R']
x = subprocess.check_output(cmd, shell=True)

I have then the following error : 我有以下错误:

...
  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) As for the path, I have checked and yes R folder is in my path. 4)至于路径,我已经检查过,是的R文件夹在我的路径中。

I newly installed python35 on my windows machine, is it a file missing in the lib subprocess, or is it something I have missed? 我在我的Windows机器上新安装了python35,它是lib子进程中缺少的文件,还是我错过了什么?

Check your python script and Rscript in are the same file location. 检查你的python脚本和Rscript是否是相同的文件位置。

Example: Create a file named sb.R and subprocess_demo.py under Desktop location. 示例:在桌面位置下创建名为sb.R和subprocess_demo.py的文件。 Change directory to Desktop in command prompt and run following command python subprocess_demo.py 在命令提示符下将目录更改为Desktop并运行以下命令python subprocess_demo.py

sb.R sb.R

c <- 3
print(paste("hello", c))

subprocess_demo.py subprocess_demo.py

import subprocess
subprocess.call(['Rscript', "sb.R"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM