Running R script with subprocess on Windows, subprocess seems missing
<p>I am using python on windows.
I want to call an R script in my python code.</p>

<p>My script R is :</p>

<pre><code>c &lt;- 3
print(paste("hello", c))
</code></pre>

<p>My python code calls the Rscript like this:</p>

<pre><code>import subprocess
subprocess.call(['Rscript', "sb.R"])
</code></pre>

<p>Unfortunately I have this error message :</p>

<pre><code>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
</code></pre>

<p>I have checked the following:</p>

<p>1) under powershell, when I enter <code>Rscript .\sb.R</code> it works.</p>

<p>2) in the python code I tried </p>

<pre><code>theproc = subprocess.Popen([sys.executable, "sbpy.py"])
theproc.communicate()
</code></pre>

<p>where sbpy.py contains <code>print("hello py")</code>. This works as well. So I assume that the library subprocess do work.</p>

<p>3) I tried the following code :</p>

<pre><code>cmd = ['Rscript', 'sb.R']
x = subprocess.check_output(cmd, shell=True)
</code></pre>

<p>I have then the following error :</p>

<pre><code>...
  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
</code></pre>

<p>4) As for the path, I have checked and yes R folder is in my path.</p>

<p>I newly installed python35 on my windows machine, is it a file missing in the lib subprocess, or is it something I have missed?</p>

<p>Check your python script and Rscript in are the same file location.</p>

<p>Example: 
       Create a file named sb.R and subprocess_demo.py under Desktop location.
       Change directory to Desktop in command prompt and run following command 
       <strong>python subprocess_demo.py</strong></p>

<p><strong><em>sb.R</em></strong></p>

<pre><code>c &lt;- 3
print(paste("hello", c))
</code></pre>

<p><strong><em>subprocess_demo.py</em></strong></p>

<pre><code>import subprocess
subprocess.call(['Rscript', "sb.R"])
</code></pre>

python windows powershell subprocess