Skip to content

Commit

Permalink
Merge pull request #1724 from rstudio/py_require-allow-opt-in
Browse files Browse the repository at this point in the history
Allow users to explicitly opt-in to selecting ephemeral uv env
  • Loading branch information
t-kalinowski authored Feb 1, 2025
2 parents d60ee07 + 56c1135 commit f6094a7
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ py_discover_config <- function(required_module = NULL, use_environment = NULL) {
reticulate_env <- Sys.getenv("RETICULATE_PYTHON", unset = NA)
if (!is.na(reticulate_env)) {

if (reticulate_env == "managed") {
return(python_config_ephemeral_uv_venv(required_module))
}

python_version <- normalize_python_path(reticulate_env)
if (!python_version$exists)
stop("Python specified in RETICULATE_PYTHON (", reticulate_env, ") does not exist")
Expand Down Expand Up @@ -222,6 +226,10 @@ py_discover_config <- function(required_module = NULL, use_environment = NULL) {
forced = "use_python() function")))
}

if (tolower(Sys.getenv("RETICULATE_USE_MANAGED_VENV")) %in% c("true", "1", "yes")) {
return(python_config_ephemeral_uv_venv(required_module))
}

# check if we're running in an activated venv
if (is_virtualenv(envpath <- Sys.getenv("VIRTUAL_ENV", NA))) {
# If this check ends up being too strict, we can alternatively do:
Expand Down Expand Up @@ -324,13 +332,7 @@ py_discover_config <- function(required_module = NULL, use_environment = NULL) {
## the Python requirements declared via `py_require()`.
user_opted_out <- tolower(Sys.getenv("RETICULATE_USE_MANAGED_VENV")) %in% c("false", "0", "no")
if (!user_opted_out) {
if (isTRUE(getOption("reticulate.python.initializing"))) {
python <- try(uv_get_or_create_env())
if (!is.null(python) && !inherits(python, "try-error"))
try(return(python_config(python, required_module, forced = "py_require()")))
}
# most likely called from py_exe()
return(NULL)
return(python_config_ephemeral_uv_venv(required_module))
}

# fall back to using the PATH python, or fail.
Expand Down Expand Up @@ -421,6 +423,16 @@ py_discover_config <- function(required_module = NULL, use_environment = NULL) {
return(NULL)
}

python_config_ephemeral_uv_venv <- function(required_module) {
if (isTRUE(getOption("reticulate.python.initializing"))) {
python <- try(uv_get_or_create_env())
if (!is.null(python) && !inherits(python, "try-error"))
try(return(python_config(python, required_module, forced = "py_require()")))
}
# most likely called from py_exe()
NULL
}

py_discover_config_fallbacks <- function() {

# prefer conda python if available
Expand Down

0 comments on commit f6094a7

Please sign in to comment.