python - 在 Windows 上运行带有子进程的 R 脚本,子进程似乎丢失

标签 python r windows powershell subprocess

我在 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 的文件。 在命令提示符下将目录更改为桌面并运行以下命令 python subprocess_demo.py

sb.R

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

subprocess_demo.py

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

关于python - 在 Windows 上运行带有子进程的 R 脚本,子进程似乎丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39445422/

相关文章:

python - 指纹识别远程操作系统 : Python

r - 使用 R 更改具有 0 和 1 信息的矩阵中的数据帧

windows - 如何在远程桌面服务器启动时自动启动并运行 tomcat 6.0.37 版本作为后台进程?

c - Windows 上存在多少个独立的文件指针/位置?

c++ - 退出应用程序时崩溃

python - 将列值转换为列?

python - Python 函数式编程最佳实践的静态分析?

python - 使用 Python 单击网站按钮

r - 提取列标题

r - 在R中的for循环中存储日期增量的输出