Python - 使用子进程来传输 R 脚本,结果是 Winerror 2

Python - Using subprocess to Pipe R Script, resultant Winerror 2

我正在尝试使用 subprocess 通过 Python 运行 R 脚本,但不幸的是,我不断收到以下错误:

WindowsError: [Error 2] The system cannot find the file specified

这非常令人沮丧,因为我已经多次检查了所有内容的路径(在没有 C:/ 的情况下完成,移动到不同的目录,更改了我尝试 运行 的 R 脚本等) .我还检查了 CompSec 环境变量,它也是正确的(通过 Windows 系统,尽管我从未在我的 IDE、Spyder 中直接检查过它)。我已经尝试在 Python 2.7 和 Python 3.5 中执行此操作,但两个版本均无效。

代码如下:

import subprocess

def sort_ByInputColumn(inputPath, inputFileTermination, sortColumn, outputPath, outputFileTermination):
scriptPath = "C:/Users/Kyle/Documents/Rscript_SortByInputColumn.R"
subprocess.call(["Rscript",  scriptPath, inputPath, inputFileTermination, sortColumn, outputPath, outputFileTermination])

fileName = 'Alabama'
outputPath = "C:/Users/Kyle/Documents/HillData/Data/Output/Module2/"
sortColumn = str(16)
inputTermination = fileName + 'Module2NN_WorkCounty_Work.csv'
outputFileTermination = fileName + 'Module2NN_SortedWorkCounty.csv'
sort_ByInputColumn(outputPath, inputTermination, sortColumn, outputPath, outputFileTermination)

无论我尝试使用什么代码 运行(甚至公然复制粘贴 this tutorial 以尝试使其工作),我都会收到此错误这一事实让我觉得更深入(或一些非常明显的事情)正在发生但我没有看到。

非常感谢任何关于此事的反馈。

为了解决这个最恼人和可怕的问题,我将 R 重新安装到一个没有 space 的目录中,并使用其完整路径调用 Rscript(即 "C:/R/R-3.3.1/bin/Rscript.exe" 来自 C:/Program Files/R/R-3.3.1/bin/Rscript.exe" 因为 Program Files 有一个 space 而这会杀死 subprocess 因为它是基于命令行的,我猜)。这一次,它奏效了。

请参阅 了解我从何处获得灵感的提示。