Background
I have a Python script, pythonScript.py
that I want to source via an R script, rScript.r
.
I use VS Studio Code Insiders to run my R scripts, specifically using the radian console; months ago, I followed this tutorial (or another similar tutorial) to set up my environment.
Problem
When using functions from the reticulate
package, an error is thrown:
ModuleNotFoundError: No module named 'rpytools'
.
In an effort to make a minimal reproducible example, I will use the py_config
function from reticulate
, although ultimately my goal is to use source_python
to run my pythonScript.py
.
Input:
install.packages("reticulate")
reticulate::use_python("<redacted>/anaconda3/python.exe")
reticulate::py_config()
# The rest WILL NOT run in radian from VS Code Insiders
1+1
Output:
Error in py_module_import(module, convert = convert) :
ModuleNotFoundError: No module named 'rpytools'
I tried locating and moving the location of the rpytools
folder, but regardless of where I moved it to, I couldn't get around the error. I also tried uninstalling and re-installing reticulate
, but I never managed to get around this error while using VS Code.
Two sub-optimal work-arounds:
- If I run the same code in R Studio, there is no error and it executes fine. However, I would like to keep my project in VS Code.
- In VS Code, if I wrap the
py_config
step in try
and then run it again (in the same R session), the script finishes running without a problem! This feels 'hacky' and I would like to avoid using this tactic to get my script to execute.
Input:
install.packages("reticulate")
reticulate::use_python("<redacted>/anaconda3/python.exe")
# The rest WILL run
try(reticulate::py_config())
reticulate::py_config()
1+1
Output:
Error in py_module_import(module, convert = convert) :
ModuleNotFoundError: No module named 'rpytools'
python: <redacted>/anaconda3/python.exe
libpython: <redacted>/anaconda3/python38.dll
pythonhome: <redacted>/anaconda3
version: 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]
Architecture: 64bit
numpy: <redacted>/anaconda3/Lib/site-packages/numpy
numpy_version: 1.19.1
NOTE: Python version was forced by RETICULATE_PYTHON
[1] 2
Questions:
- Why does the error only arise when using VS Code Studio Insiders / radian but not in R Studio?
- Why does it fail on the first call of
py_config
(or other reticulate
functions) in a new R session, but it does not fail on subsequent calls in the same R session?
- How can I 'fix' this so that it will run on the first call in VS Code?
Related posts:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…