rshellrstudioreticulate

running Python script using system2() works in R terminal but not RStudio


Given a simple python script, "test.py":

from sys import stdout

stdout.write("text out")

Calling this from the R terminal using system2() works as expected:

python <- "C:/Users/username/Anaconda3/envs/general/python.exe"
script <- "C:/Users/username/Project Folder/test.py"
system2(command = python, args = shQuote(script), stdout = TRUE, stderr = FALSE)
# "text out"

But running the same code in RStudio returns an empty vector with a non-zero exit warning:

system2(command = python, args = shQuote(script), stdout = TRUE, stderr = FALSE)
# character(0)
# attr(,"status")
# [1] 1
# Warning message:
# In system2(command = python, args = shQuote(script), stdout = TRUE,  :
#   running command '"C:/Users/username/Anaconda3/envs/general/python.exe" "C:/Users/username/Project Folder/test.py"' had status 1

Additional troubleshooting shows:

The context for this is that I've never been able to get reticulate to work on my machine, despite periodically trying across several versions of R and RStudio. The problem seems to stem from a system2() call similar to the above in reticulate:::python_config_impl(), which yields the same warning (and subsequently an error).


Solution

  • I still don't understand what the root cause is, but I've found a workaround -- everything works fine if I launch RStudio from a batch script rather than a shortcut.

    start "" "C:\Program Files\RStudio\rstudio.exe"
    

    Since this works, I assumed there must be some difference in the environment when launched from a command prompt / batch rather than the shortcut. But editing ".Renviron" to make all env variables match what I see when launching from batch didn't help. So I still don't really know what the problem is or why this solution works.