You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My R package can pass R CMD Check and local tests of package loading during installation. However, if I try to load my package using library(mypkg), R Studio global settings seem to conflict with my code insde the .onLoad().
How to Reproduce
In my R package, in .onLoad(), I have these lines:
## Code to configure the virtual environment and install pandas ## Thenreticulate::use_virtualenv("env_name")
reticulate::source_python(system.file(paste0("model/", "import.py"), package="mypkg"))
And in import.py I have:
importpandasaspd
In R Studio -> Global Options -> Python -> Python interpreter, if the path is set to a system level Python installation, I always get an error when loading my package:
If I restart R Studio and activate my virtual environment before loading my package, it works but there is a warning:
reticulate::use_virtualenv("env_name")
Warningmessage:Previousrequestto`use_python("C:/Users/xxx/AppData/Local/Programs/Python/Python310/python.exe", required = TRUE)`willbeignored.Itissupersededbyrequestto `use_python("C:/Users/xxx/.virtualenvs/env_name/Scripts/python.exe")
library(mypkg) # now it works
Solution
Clear the Python interpreter path in R Studio -> Global Options -> Python -> Python interpreter.
Alternatively, run use_virtualenv("env_name") before library(mypkg)
Question
Why use_virtualenv() works manually but not inside .onLoad()?
Is there a conflict between R Studio global settings and reticulate? Or, is it a bad practice to use use_virtualenv() and source_python() inside .onLoad()?
The text was updated successfully, but these errors were encountered:
Thanks for opening. The default value of the required argument in use_virtualenv(), use_python(), and use_conda() is different within .onLoad(). It is required = TRUE in most contexts, except in .onLoad(), where it is required = FALSE.
This is primarily for historical reasons. The default used to be required = FALSE. However, when we changed the default to required = TRUE, we preserved the behavior of required = FALSE in .onLoad() to avoid introducing breaking changes in already published packages and because most R packages generally should not place hard requirements on a specific Python environment.
Additionally, we do not recommend using source_python() within .onLoad() because it modifies the user's global environment and forces reticulate to initialize Python, possibly before the user has an opportunity to specify which Python should be selected. Using import_from_path(delay_load = TRUE) might be a better approach.
Problem
My R package can pass R CMD Check and local tests of package loading during installation. However, if I try to load my package using
library(mypkg)
, R Studio global settings seem to conflict with my code insde the.onLoad()
.How to Reproduce
In my R package, in
.onLoad()
, I have these lines:And in import.py I have:
In R Studio -> Global Options -> Python -> Python interpreter, if the path is set to a system level Python installation, I always get an error when loading my package:
If I manually activate my virtual environment after this error, I get:
If I restart R Studio and activate my virtual environment before loading my package, it works but there is a warning:
Solution
use_virtualenv("env_name")
beforelibrary(mypkg)
Question
Why
use_virtualenv()
works manually but not inside.onLoad()
?Is there a conflict between R Studio global settings and
reticulate
? Or, is it a bad practice to useuse_virtualenv()
andsource_python()
inside.onLoad()
?The text was updated successfully, but these errors were encountered: