【问题标题】:Error while running R script in Python with SubProcess使用 SubProcess 在 Python 中运行 R 脚本时出错
【发布时间】:2018-05-18 02:06:10
【问题描述】:

在执行 python 脚本时,我正在尝试运行与 python 脚本位于同一目录中的 R 脚本。

到目前为止,我有:

if condition is True:
    import subprocess
    subprocess.call (["C:/Program Files/R/R-3.4.3/Rscript", "./testing.r"])
    sys.exit()

我不断收到错误:

OSError: [WinError 193] %1 is not a valid Win32 application

我尝试将“C:/Program Files/R/R-3.4.3/Rscript”替换为“/usr/bin/Rscript”,但仍然出现同样的错误。我想知道是否有人会知道为什么它一直抛出这个错误?

【问题讨论】:

  • 确定不应该用cmd提交吗?
  • 试试这个subprocess.call(['C:/Program Files/R/R-3.4.3/Rscript', '--vanilla', 'testing.r'], shell = True) 或提供 R 脚本文件的完整路径
  • 我猜这可能是路径问题。 (R 版本和 Rscript 路径肯定是正确的?)如果是,那么给出 testing.r 的完整绝对路径,所以 'C:/full/absolute/path/to/testing.r' (或者它在 ' D:/'偶数)?
  • 我已经尝试了这两个建议,但得到一个错误,“C:/Program Files/R/R-3.4.3/Rscript”不被识别为外部或内部命令。

标签: python r subprocess


【解决方案1】:

我相信 subprocess.call 的参数会直接传递到命令行,因此您需要将引号转义为 "\"C:/Program Files/R/R-3.4.3/Rscript\"" .话虽如此,当我使用它时,我得到一个 [WinError 5] 访问冲突错误。一种解决方法是使用 executable 参数:

import sys
import subprocess

if True is True:
    subprocess.call(["C:/Program Files/R/R-3.4.3/Rscript.exe", "./testing.r"], 
                    executable="C:/Program Files/R/R-3.4.3/Rscript.exe")
    sys.exit()

另外,请确保 C:/Program Files/R/R-3.4.3/Rscript.exe 是您的 Rscript.exe 的位置。我的是 C:/Program Files/R/R-3.4.3/bin/Rscript.exe。

【讨论】:

  • 是的,我错过了“C:/Program Files/R/R-3.4.3/bin/Rscript.exe”中的“bin”。谢谢!
猜你喜欢
  • 2022-01-01
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多