2

I'm trying to run an R Script through Python using subprocess but unfortunately, I'm continually getting the following error:

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

This is extremely frustrating as I've checked the path over everything multiple times (done it without C:/, moved to different directories, changed the R script I'm trying to run, etc). I've also checked the CompSec Environment Variable, and it is also correct (through Windows Systems, although I never checked it directly in my IDE, Spyder). I've tried doing it in Python 2.7 and Python 3.5, and neither version works.

The code goes as follows:

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)

The fact that I get this error no matter what code I try to run (even blatantly copy-pasting this tutorial to try and make it work) makes me feel like something deeper (or something extremely obvious) is going on that I'm not seeing.

Would appreciate any feedback on the matter.

genap
  • 165
  • 1
  • 11
  • Can you run the command in cmd? – Natecat Jun 25 '16 at 03:59
  • Try providing the full path to the executable (assuming _Rscript_ is the program you want to run). – handle Jun 25 '16 at 07:30
  • Prefix your script command by "cmd /c". Unless it is an executable, the `subprocess` module won't exec it for you (unlike `os.system`) – Jean-François Fabre Jun 25 '16 at 07:36
  • @handle I tried doing that, I got the same error. – genap Jun 25 '16 at 08:14
  • @Jean-FrançoisFabre What do you mean? – genap Jun 25 '16 at 08:14
  • @genap Jean-FrançoisFabre is pointing out that subprocess does not resolve which executable to open your script with (system default handler). But this does not apply here as you are providing the executable, albeit without absolut path (so it at least needs to be in environment variable). – handle Jun 25 '16 at 08:39
  • @genap Try it as Natecat suggests: print( " ".join( callist ) ) and run it manually. Try to find out which args are actually passed to the target program. Try quoting path arguments or convert them to local abs path. – handle Jun 25 '16 at 08:44

1 Answers1

2

To solve this most annoying and horrendous problem, I reinstalled R into a directory with no spaces and called Rscript with its full pathing (that is "C:/R/R-3.3.1/bin/Rscript.exe" from C:/Program Files/R/R-3.3.1/bin/Rscript.exe" because Program Files has a space and this kills subprocess because it's command line based, I guess). This time, it worked.

See this similar question for a hint as to where I got the inspiration for this.

Community
  • 1
  • 1
genap
  • 165
  • 1
  • 11