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:
print()
or tries writing to disk instead of stdout.write()
.system(paste(shQuote(python), shQuote(script)))
instead of system2()
.system2()
in RStudio works fine to run a system command or a batch file with arguments -- just not Python scripts.system2("printenv")
shows a few env variables set in the R terminal session and not in RStudio; but setting these in RStudio using Sys.setenv()
doesn't solve the problem. sessionInfo()
shows no obvious differences.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).
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.