Skip to content

"Automatically activate project-local Python environments" activates system python. #13497

Closed

Description

@t-kalinowski

On a fresh installation of the IDE and reticulate on macOS, reticulate will default to discovering the system python /usr/bin/python3 in the IDE. This is not consistent with the guidance we provide elsewhere for users to prefer working in virtual environments.

This happens because the option "Python > Automatically activate project-local Python environments" is checked by default. When this option is checked, even in projects where there is no project-local configured python, the IDE will set env var RETICULATE_PYTHON_FALLBACK, to what it finds: /usr/bin/python3.

When the system python is selected by the IDE like this, it also adjusts PATH by prepending /usr/bin, masking manually installed and preferred executables under /usr/local/bin.
(e.g., https://positpbc.slack.com/archives/CTACV7WC9/p1688650055493939?thread_ts=1688588487.673509&cid=CTACV7WC9, #12787)

If the IDE did not set this env var in this situation, then reticulate would continue along the documented "Order of Discovery", prompt the the user to create a "r-reticulate" venv, and default to using that.

It's not clear to me what is best to do here. Some loose thoughts about potential changes:

  • Maybe this checkbox should be unchecked by default? With the latest release, reticulate will now automatically discover a project local venv, perhaps the IDE doesn't need to do this by default anymore?
  • If we leave it checked by default, maybe the checkbox should only automatically discover interpreters in the project directory, and not venture out of the project directory to find the system python, unless explicitly asked to do so by the user? If checked, and not pointed at a specific python by the user, and if the IDE doesn't find a python in the project directory, then perhaps the IDE should leave the env var RETICULATE_PYTHON_FALLBACK (and other env vars like VIRTUAL_ENV) unset.
  • If the checkbox is checked, the "Python Interpreter" box in the GUI preference pane should indicate which interpreter is being set by the IDE, instead of "(No interpreter selected)".
  • Maybe, if we default it to off, the IDE option should switch to setting RETICULATE_PYTHON instead of RETICULATE_PYTHON_FALLBACK for the user? Note that VIRTUAL_ENV, also set by the IDE when the checkbox is checked, is earlier in reticulates order of discovery than RETICULATE_PYTHON_FALLBACK.

Activity

  1. added
    new
    on Aug 15, 2023
  2. removed
    new
    on Aug 15, 2023
  3. added this to the Desert Sunflower milestone on Aug 15, 2023
  4. kevinushey commented on Sep 5, 2023

    @kevinushey
    Collaborator

    For reference:

    .rs.addFunction("python.configuredInterpreterPath", function(projectDir)
    {
    # on Windows, check if PY_PYTHON is defined; if it is, we should use 'py'
    # to determine the version of python to be used
    if (.rs.platform.isWindows)
    {
    pyPython <- Sys.getenv("PY_PYTHON", unset = NA)
    if (!is.na(pyPython))
    {
    python <- .rs.python.findWindowsPython()
    if (file.exists(python))
    return(python)
    }
    }
    # check some pre-defined environment variables
    vars <- c("RENV_PYTHON", "RETICULATE_PYTHON", "RETICULATE_PYTHON_FALLBACK")
    for (var in vars)
    {
    value <- Sys.getenv(var, unset = NA)
    if (!is.na(value))
    return(value)
    }
    # if this project has a local interpreter, use it
    if (!is.null(projectDir))
    {
    projectPython <- .rs.python.projectInterpreterPath(projectDir)
    if (file.exists(projectPython))
    return(projectPython)
    }
    # check version of Python configured by user
    prefsPython <- .rs.readUiPref("python_path")
    if (file.exists(prefsPython))
    return(path.expand(prefsPython))
    # on Windows, help users find a default version of Python if possible
    if (.rs.platform.isWindows)
    {
    pythonPath <- .rs.python.findWindowsPython()
    if (file.exists(pythonPath))
    return(pythonPath)
    }
    # look for python + python3 on the PATH
    if (!.rs.platform.isWindows)
    {
    python3 <- Sys.which("python3")
    if (nzchar(python3) && python3 != "/usr/bin/python3")
    return(python3)
    python <- Sys.which("python")
    if (nzchar(python) && python != "/usr/bin/python")
    {
    info <- .rs.python.interpreterInfo(python, NULL)
    version <- numeric_version(info$version, strict = FALSE)
    if (!is.na(version) && version >= "3.2")
    return(python)
    }
    }
    # if the user has Anaconda installed, then try auto-activating
    # the base environment of that Anaconda installation
    conda <- .rs.python.findCondaBinary()
    if (file.exists(conda))
    {
    pythonPath <- if (.rs.platform.isWindows) "../python.exe" else "../bin/python"
    python <- file.path(dirname(conda), pythonPath)
    if (file.exists(python))
    return(python)
    }
    # fall back to versions of python in /usr/bin if available
    if (!.rs.platform.isWindows)
    {
    python3 <- Sys.which("python3")
    if (nzchar(python3) && python3 == "/usr/bin/python3")
    return(python3)
    python <- Sys.which("python")
    if (nzchar(python) && python == "/usr/bin/python")
    {
    info <- .rs.python.interpreterInfo(python, NULL)
    version <- numeric_version(info$version, strict = FALSE)
    if (!is.na(version) && version >= "3.2")
    return(python)
    }
    }
    # no python found; return empty string placeholder
    ""
    })

    Maybe we should remove the code here?

    # fall back to versions of python in /usr/bin if available
    if (!.rs.platform.isWindows)
    {
    python3 <- Sys.which("python3")
    if (nzchar(python3) && python3 == "/usr/bin/python3")
    return(python3)
    python <- Sys.which("python")
    if (nzchar(python) && python == "/usr/bin/python")
    {
    info <- .rs.python.interpreterInfo(python, NULL)
    version <- numeric_version(info$version, strict = FALSE)
    if (!is.na(version) && version >= "3.2")
    return(python)
    }
    }

  5. jonvanausdeln commented on Sep 12, 2023

    @jonvanausdeln

    Since we are so close to Desert Sunflower release, moving to Ocean Storm

  6. self-assigned this
    on Sep 20, 2023
  7. jonvanausdeln commented on Nov 7, 2023

    @jonvanausdeln

    Verified with 2023.12.0-daily+266
    MacOS 13, Ubuntu22

    The correct Python interpreter appears to be activated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions