Skip to content

"Automatically activate project-local Python environments" preference can put /usr/bin at front of PATH #12787

Closed

Description

@kevinushey

By default, RStudio tries to place the configured copy of python on the PATH.

Screenshot 2023-03-03 at 3 26 59 PM

However, there are a couple of issues.

  1. The preference is called "automatically activate project-local Python environments", but it seems like we're trying to activate non-local Python installations as well.

  2. If we're trying to activate a "system" copy of Python, we end up putting that directory on the PATH as well. This implies that if python is installed at /usr/bin/python, then we'll end up prepending /usr/bin to the PATH, which is undesired.

For reference, the code handling this logic lives here:

.rs.addFunction("python.initialize", function(projectDir)
{
# do nothing if the user hasn't opted in
activate <- .rs.readUiPref("python_project_environment_automatic_activate")
if (!identical(activate, TRUE))
return()
# find path to python interpreter for this project
pythonPath <- .rs.python.configuredInterpreterPath(projectDir)
if (!file.exists(pythonPath))
return()
# normalize path (avoid following symlinks)
pythonPath <- file.path(
normalizePath(dirname(pythonPath), winslash = "/", mustWork = FALSE),
basename(pythonPath)
)
# add the Python directory to the PATH
pythonBin <- dirname(pythonPath)
.rs.prependToPath(pythonBin)
# if we have a Scripts directory, place that on the PATH as well
# (primarily for Windows + conda environments)
scriptsPath <- file.path(pythonBin, "Scripts")
if (file.exists(scriptsPath))
.rs.prependToPath(scriptsPath)
pythonInfo <- .rs.python.getPythonInfo(pythonPath, strict = TRUE)
# if this is a virtual environment, set VIRTUAL_ENV
if (identical(pythonInfo$type, "virtualenv"))
{
envPath <- dirname(dirname(pythonPath))
Sys.setenv(VIRTUAL_ENV = envPath)
}
# if this is a conda environment, set CONDA_PREFIX
if (identical(pythonInfo$type, "conda"))
{
condaPrefix <- pythonBin
if (!.rs.platform.isWindows)
condaPrefix <- dirname(condaPrefix)
Sys.setenv(CONDA_PREFIX = condaPrefix)
# also ensure that conda is placed on the PATH
condaPath <- .rs.python.findCondaForEnvironment(condaPrefix)
if (file.exists(condaPath))
.rs.prependToPath(dirname(condaPath))
}
# also set RETICULATE_PYTHON_FALLBACK so this python is used by default
Sys.setenv(RETICULATE_PYTHON_FALLBACK = pythonPath)
# return path to python
invisible(pythonPath)
})

See also; Pro ticket 84885; Pro ticket 85583

Activity

  1. added
    new
    on Mar 3, 2023
  2. added and removed
    new
    on Mar 6, 2023
  3. mikebessuille commented on Mar 6, 2023

    @mikebessuille
    Contributor

    Possibly related to #10997 ?

  4. added this to the Backlog-1 milestone on Mar 6, 2023
  5. aronatkins commented on Jul 6, 2023

    @aronatkins
    Contributor

    This PATH adjustment causes problems on systems that need to locate tools installed into /usr/local/bin (e.g. a homebrew-installed version of tidy). With this option selected, the macOS installed /usr/bin/tidy is always used. This old version of tidy incorrectly reports a number of issues when running devtools::check(remote = TRUE, manual = TRUE) against R packages.

  6. DavisVaughan commented on Jul 31, 2023

    @DavisVaughan

    I just had exactly the same issue as @aronatkins with my new Mac

    i.e.

    Screenshot 2023-07-31 at 8 54 45 AM

    Where Restarting R session... also included me turning off Automatically activate project-local Python environments

  7. modified the milestones: Backlog-1, Backlog on Jan 23, 2025
  8. self-assigned this
    on Feb 6, 2025
  9. kevinushey commented on Feb 6, 2025

    @kevinushey
    CollaboratorAuthor

    I think this was resolved in #13497.

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