Open EvoLandEco opened 1 month ago
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()
?