Skip to content

Commit

Permalink
Merge pull request #1673 from rstudio/fix/windows-appstore-python
Browse files Browse the repository at this point in the history
Fix error when encountering Windows App Store Python
  • Loading branch information
t-kalinowski authored Sep 18, 2024
2 parents 5dcad1f + 1141b5c commit 4753612
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# reticulate (development version)

- Fixed error where `py_discover_config()` attempted to detect
Windows App Store Python installations, which are now excluded
from discovery by both `py_discover_config()` and `virtualenv_starter()`
(#1656, #1673).

- Fixed error when converting an empty NumPy char array to R (#1662).

- Fixed error when using reticulate with radian (#1668, #1670).
Expand Down
34 changes: 30 additions & 4 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,25 @@ py_discover_config <- function(required_module = NULL, use_environment = NULL) {
size <- ifelse(is.na(info$size), 0, info$size)
python_versions <- python_versions[size != 0]


# We should not automatically discover windows app store python
python_versions <-
python_versions[!is_windows_app_store_python(python_versions)]

# remove msys2 / cygwin python executables.
# path translation going to and from msys2 currently not implemented.
# E.g.: "C:\foo\bar" -> "/c/foo/bar" and "/foo/bar" -> "C:\rtools43\foo\bar"
# https://github.com/rstudio/reticulate/issues/1325
python_sys_platforms <- vapply(
python_versions, system2, "",
args = c("-c", shQuote("import sys; print(sys.platform)")),
stdout = TRUE)
get_platform <- function(python) {
tryCatch({
system2(python,
args = c("-c", shQuote("import sys; print(sys.platform)")),
stdout = TRUE, stderr = FALSE)
}, warning = function(w) "", error = function(e) "")
}
python_sys_platforms <- vapply(python_versions, get_platform, "")

python_versions <- python_versions[python_sys_platforms != ""]
python_versions <- python_versions[python_sys_platforms != "cygwin"]
}

Expand Down Expand Up @@ -1235,3 +1245,19 @@ py_session_initialized_binary <- function() {
# return
python_binary
}


is_windows_app_store_python <- function(python) {
# There is probably a better way, but don't currently have
# access to a windows machine with the app store installed.
python <- normalizePath(python, winslash = "/", mustWork = FALSE)
grepl("/Program Files/WindowsApps/PythonSoftwareFoundation.Python",
python, fixed = TRUE)
}


find_all_pythons <- function(root = "/") {
cmd <- sprintf("find %s -type f -regex '.*/python[0-9.]*$' -executable 2>/dev/null",
root)
as.character(suppressWarnings(system(cmd, intern = TRUE)))
}
5 changes: 5 additions & 0 deletions R/virtualenv.R
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,11 @@ virtualenv_starter <- function(version = NULL, all = FALSE) {
rownames(starters) <- NULL
}

if (is_windows()) {
# Windows App Store Python should never be used.
starters <- starters[!is_windows_app_store_python(starters$path), ]
}

if (all)
starters
else if (nrow(starters))
Expand Down

0 comments on commit 4753612

Please sign in to comment.