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.

7
  • Can you run the command in cmd? Commented Jun 25, 2016 at 3:59
  • Try providing the full path to the executable (assuming Rscript is the program you want to run). Commented Jun 25, 2016 at 7: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) Commented Jun 25, 2016 at 7:36
  • @handle I tried doing that, I got the same error. Commented Jun 25, 2016 at 8:14
  • @Jean-FrançoisFabre What do you mean? Commented Jun 25, 2016 at 8:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.