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:
- In Rgui, there's no warning, but also returns just an empty vector.
- Results are the same if the Python script uses
print()
or tries writing to disk instead ofstdout.write()
. - Results are the same using
system(paste(shQuote(python), shQuote(script)))
instead ofsystem2()
. - Using
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 usingSys.setenv()
doesn't solve the problem.sessionInfo()
shows no obvious differences.- The problem persists if RStudio is run with admin permissions.
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).