1

I run:

Rscript hello_world.R

from my cmd terminal where the directory is:

C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara>

The script runs fine.

However, I want Python to run it and have this small script located in the same directory as above:

import subprocess
subprocess.run(['Rscript', 'hello_world.R'])

However, I get this error when I run it from VS Code:

Exception has occurred: FileNotFoundError
[WinError 2] The system cannot find the file specified

I next tried:

subprocess.run(['Rscript', 'hello_world.R'], shell=True)

But I got:

'Rscript' is not recognized as an internal or external command, operable program or batch file.

For reference I've added the following to my path:

C:\Program Files\R\R-3.6.3\bin\x64

I've just tried running

Rscript hello_world.R

from the cmd terminal in VS Code which has the following directory set:

(polgara) C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara>

This also gives the error:

'Rscript' is not recognized as an internal or external command, operable program or batch file.

Could the (polgara) at the beginning be the source of my problem? I believe this is my virtual environment...?

Jossy
  • 589
  • 2
  • 12
  • 36
  • What matters is the *current working* directory, which is **not** necessarily the same directory your code is in. That is to say, if you're in `C:/` and you run `c:/hello/something.py`, and it then tries to open `hello_world.R`, it'll look for `C:/hello_world.r`, not `c:/hello/hello_world.r` (forward-slashes used for markdown sanity) – Charles Duffy Jul 12 '20 at 17:41
  • ...whereas when you get an error about `Rscript` not being found, that means it's time to look at your `PATH` *for the specific process throwing the error*, as opposed to paying attention to general system-wide defaults. Inspect `os.environ['PATH']`. (Yes, `(polgara)` tells you you're in a virtualenv, and entering a virtualenv changes your PATH, so your active PATH is likely to differ from the system-wide defaults). – Charles Duffy Jul 12 '20 at 17:43
  • Hi @CharlesDuffy - thanks for replying. Afraid I'm pretty new to everything command line. How do I make sure my Python script is using the right working directory? How do I look at my ```PATH``` for a specific process throwing an error? What do you mean when you say virtualenv change my ```PATH```? What implications does that have? Are you able to give me some simple instructions on what to do? Thanks again. – Jossy Jul 12 '20 at 17:50
  • Restarting the machine is overkill -- just logging out and back in would have worked. Restarting individual processes is _sometimes_ good enough too; "log all the way out" is just convenient as a way to make sure _everything_ has been started. (Processes initialize environment variables from their parents at the time when they're started; global changes don't modify an individual process's environment after-the-fact). – Charles Duffy Jul 13 '20 at 15:00
  • ...btw, to inspect your PATH from Python, `print(os.environ['PATH'])`. – Charles Duffy Jul 13 '20 at 15:02

1 Answers1

1

Please take a look on:

What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?

It is necessary to restart Windows after a modification of system environment variable Path to make sure all processes work with updated Path.

In general any executable can be run from within a Python script without the usage of Windows command processor cmd.exe which means without using shell=True. But it is necessary to specify the executable with full qualified file name, i.e. drive + path + name + extension, on not being in current directory of running process which is in this case python.exe interpreting the Python script.

I suggest to read the Microsoft documentation for function CreateProcessA and the other pages referenced on this page respectively listed on left side. All the parameters of the methods of Python module subprocess like cwd (current working directory for the subprocess) are easier to understand on having knowledge about process creation by Windows kernel.

The R script file name passed as argument to Rscript.exe should be specified also with full qualified file name to be found by Rscript.exe independent on what is the current directory of Rscript process.

Mofi
  • 46,139
  • 17
  • 80
  • 143