| 1 |
|
| 2 |
#' Use python |
| 3 |
#' |
| 4 |
#' Associate a version of Python with your project. |
| 5 |
#' |
| 6 |
#' When Python integration is active, renv will: |
| 7 |
#' |
| 8 |
#' - Save metadata about the requested version of Python in `renv.lock` -- in |
| 9 |
#' particular, the Python version, and the Python type ("virtualenv", "conda", |
| 10 |
#' "system"), |
| 11 |
#' |
| 12 |
#' - Capture the set of installed Python packages during `renv::snapshot()`, |
| 13 |
#' |
| 14 |
#' - Re-install the set of recorded Python packages during `renv::restore()`. |
| 15 |
#' |
| 16 |
#' In addition, when the project is loaded, the following actions will be taken: |
| 17 |
#' |
| 18 |
#' - The `RENV_PYTHON` environment variable will be set, indicating the version |
| 19 |
#' of Python currently active for this sessions, |
| 20 |
#' |
| 21 |
#' - The `RETICULATE_PYTHON` environment variable will be set, so that the |
| 22 |
#' reticulate package can automatically use the requested copy of Python |
| 23 |
#' as appropriate, |
| 24 |
#' |
| 25 |
#' - The requested version of Python will be placed on the `PATH`, so that |
| 26 |
#' attempts to invoke Python will resolve to the expected version of Python. |
| 27 |
#' |
| 28 |
#' You can override the version of Python used in a particular project by |
| 29 |
#' setting the `RENV_PYTHON` environment variable; e.g. as part of the |
| 30 |
#' project's `.Renviron` file. This can be useful if you find that renv |
| 31 |
#' is unable to automatically discover a compatible version of Python to |
| 32 |
#' be used in the project. |
| 33 |
#' |
| 34 |
#' @inherit renv-params |
| 35 |
#' |
| 36 |
#' @param ... Optional arguments; currently unused. |
| 37 |
#' |
| 38 |
#' @param python |
| 39 |
#' The path to the version of Python to be used with this project. See |
| 40 |
#' **Finding Python** for more details. Alternatively, set `python = FALSE` |
| 41 |
#' to deactivate Python integration for the project -- this removes the |
| 42 |
#' `Python` section from `renv.lock`. |
| 43 |
#' |
| 44 |
#' @param type |
| 45 |
#' The type of Python environment to use. When `"auto"` (the default), |
| 46 |
#' virtual environments will be used. |
| 47 |
#' |
| 48 |
#' @param name |
| 49 |
#' The name or path that should be used for the associated Python environment. |
| 50 |
#' If `NULL` and `python` points to a Python executable living within a |
| 51 |
#' pre-existing virtual environment, that environment will be used. Otherwise, |
| 52 |
#' a project-local environment will be created instead, using a name |
| 53 |
#' generated from the associated version of Python. |
| 54 |
#' |
| 55 |
#' @details |
| 56 |
#' # Finding Python |
| 57 |
#' |
| 58 |
#' In interactive sessions, when `python = NULL`, renv will prompt for an |
| 59 |
#' appropriate version of Python. renv will search a pre-defined set of |
| 60 |
#' locations when attempting to find Python installations on the system: |
| 61 |
#' |
| 62 |
#' - `getOption("renv.python.root")`, |
| 63 |
#' - `/opt/python`, |
| 64 |
#' - `/opt/local/python`, |
| 65 |
#' - `~/opt/python`, |
| 66 |
#' - `/usr/local/opt` (for macOS Homebrew-installed copies of Python), |
| 67 |
#' - `/opt/homebrew/opt` (for M1 macOS Homebrew-installed copies of Python), |
| 68 |
#' - `~/.pyenv/versions`, |
| 69 |
#' - Python instances available on the `PATH`. |
| 70 |
#' |
| 71 |
#' In non-interactive sessions, renv will first check the `RETICULATE_PYTHON` |
| 72 |
#' environment variable; if that is unset, renv will look for Python on the |
| 73 |
#' `PATH`. It is recommended that the version of Python to be used is explicitly |
| 74 |
#' supplied for non-interactive usages of `use_python()`. |
| 75 |
#' |
| 76 |
#' |
| 77 |
#' # Warning |
| 78 |
#' |
| 79 |
#' We strongly recommend using Python virtual environments, for a few reasons: |
| 80 |
#' |
| 81 |
#' 1. If something goes wrong with a local virtual environment, you can safely |
| 82 |
#' delete that virtual environment, and then re-initialize it later, without |
| 83 |
#' worry that doing so might impact other software on your system. |
| 84 |
#' |
| 85 |
#' 2. If you choose to use a "system" installation of Python, then any packages |
| 86 |
#' you install or upgrade will be visible to any other application that |
| 87 |
#' wants to use that same Python installation. Using a virtual environment |
| 88 |
#' ensures that any changes made are isolated to that environment only. |
| 89 |
#' |
| 90 |
#' 3. Choosing to use Anaconda will likely invite extra frustration in the |
| 91 |
#' future, as you may be required to upgrade and manage your Anaconda |
| 92 |
#' installation as new versions of Anaconda are released. In addition, |
| 93 |
#' Anaconda installations tend to work poorly with software not specifically |
| 94 |
#' installed as part of that same Anaconda installation. |
| 95 |
#' |
| 96 |
#' In other words, we recommend selecting "system" or "conda" only if you are an |
| 97 |
#' expert Python user who is already accustomed to managing Python / Anaconda |
| 98 |
#' installations on your own. |
| 99 |
#' |
| 100 |
#' |
| 101 |
#' @return |
| 102 |
#' `TRUE`, indicating that the requested version of Python has been |
| 103 |
#' successfully activated. Note that this function is normally called for its |
| 104 |
#' side effects. |
| 105 |
#' |
| 106 |
#' |
| 107 |
#' @export |
| 108 |
#' |
| 109 |
#' @examples |
| 110 |
#' \dontrun{ |
| 111 |
#' |
| 112 |
#' # use python with a project |
| 113 |
#' renv::use_python() |
| 114 |
#' |
| 115 |
#' # use python with a project; create the environment |
| 116 |
#' # within the project directory in the '.venv' folder |
| 117 |
#' renv::use_python(name = ".venv") |
| 118 |
#' |
| 119 |
#' # use python with a pre-existing virtual environment located elsewhere |
| 120 |
#' renv::use_python(name = "~/.virtualenvs/env") |
| 121 |
#' |
| 122 |
#' # use virtualenv python with a project |
| 123 |
#' renv::use_python(type = "virtualenv") |
| 124 |
#' |
| 125 |
#' # use conda python with a project |
| 126 |
#' renv::use_python(type = "conda") |
| 127 |
#' |
| 128 |
#' # deactivate Python integration |
| 129 |
#' renv::use_python(python = FALSE) |
| 130 |
#' |
| 131 |
#' } |
| 132 |
use_python <- function(python = NULL, |
| 133 |
..., |
| 134 |
type = c("auto", "virtualenv", "conda", "system"), |
| 135 |
name = NULL, |
| 136 |
project = NULL) |
| 137 |
{ |
| 138 |
renv_scope_error_handler() |
| 139 |
renv_dots_check(...) |
| 140 |
project <- renv_project_resolve(project) |
| 141 |
|
| 142 |
# deactivate python integration when FALSE |
| 143 |
if (identical(python, FALSE)) |
| 144 |
return(renv_python_deactivate(project)) |
| 145 |
|
| 146 |
# handle 'auto' type |
| 147 |
type <- match.arg(type) |
| 148 |
if (identical(type, "auto")) |
| 149 |
type <- "virtualenv" |
| 150 |
|
| 151 |
case( |
| 152 |
type == "system" ~ renv_use_python_system(python, name, project), |
| 153 |
type == "virtualenv" ~ renv_use_python_virtualenv(python, name, project), |
| 154 |
type == "conda" ~ renv_use_python_condaenv(python, name, project) |
| 155 |
) |
| 156 |
} |
| 157 |
|
| 158 |
renv_use_python_system <- function(python, |
| 159 |
name, |
| 160 |
project) |
| 161 |
{ |
| 162 |
# retrieve python information |
| 163 |
python <- renv_python_resolve(python) |
| 164 |
version <- renv_python_version(python) |
| 165 |
info <- renv_python_info(python) |
| 166 |
|
| 167 |
# if the user ended up selecting a virtualenv or conda python, then |
| 168 |
# just activate those and ignore the 'system' request |
| 169 |
if (identical(info$type, "virtualenv")) |
| 170 |
return(renv_use_python_virtualenv(info$python, name, project)) |
| 171 |
if (identical(info$type, "conda")) |
| 172 |
return(renv_use_python_condaenv(info$python, name, project)) |
| 173 |
|
| 174 |
# for 'system' python usages, we just use the path to python |
| 175 |
# (note that this may not be portable or useful for other machines) |
| 176 |
renv_use_python_fini(info, python, version, project) |
| 177 |
} |
| 178 |
|
| 179 |
renv_use_python_virtualenv <- function(python, |
| 180 |
name, |
| 181 |
project) |
| 182 |
{ |
| 183 |
# if name has been set, check and see if it refers to an already-existing |
| 184 |
# virtual environment; if that exists, use it |
| 185 |
if (is.null(python) && !is.null(name)) { |
| 186 |
path <- renv_python_virtualenv_path(name) |
| 187 |
if (file.exists(path)) |
| 188 |
python <- renv_python_exe(name) |
| 189 |
} |
| 190 |
|
| 191 |
python <- renv_python_resolve(python) |
| 192 |
version <- renv_python_version(python) |
| 193 |
info <- renv_python_info(python) |
| 194 |
|
| 195 |
# if name is unset, and 'python' doesn't already refer to an existing |
| 196 |
# virtual environment, then we'll use a local virtual environment |
| 197 |
local <- is.null(name) && identical(info$type, "virtualenv") |
| 198 |
if (local) { |
| 199 |
name <- renv_path_aliased(info$root) |
| 200 |
if (renv_path_same(dirname(name), renv_python_virtualenv_home())) |
| 201 |
name <- basename(name) |
| 202 |
} else { |
| 203 |
name <- name %||% renv_python_envpath(project, "virtualenv", version) |
| 204 |
if (grepl("/", name, fixed = TRUE)) |
| 205 |
name <- renv_path_canonicalize(name) |
| 206 |
} |
| 207 |
|
| 208 |
# now, check to see if the python environment exists; |
| 209 |
# if it does not exist, we'll create it now |
| 210 |
vpython <- renv_use_python_virtualenv_impl(project, name, version, python) |
| 211 |
vinfo <- renv_python_info(vpython) |
| 212 |
|
| 213 |
# finish up now |
| 214 |
renv_use_python_fini(vinfo, name, version, project) |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
renv_use_python_condaenv <- function(python, |
| 219 |
name, |
| 220 |
project) |
| 221 |
{ |
| 222 |
# if python is set, see if it's already the path to a python interpreter |
| 223 |
# living within a conda environment |
| 224 |
while (!is.null(python)) { |
| 225 |
|
| 226 |
if (!is.null(name)) { |
| 227 |
fmt <- "ignoring value of name %s as python was already set" |
| 228 |
warningf(fmt, renv_path_pretty(name)) |
| 229 |
} |
| 230 |
|
| 231 |
# validate that this is a conda python |
| 232 |
info <- renv_python_info(python) |
| 233 |
if (!identical(info$type, "conda")) { |
| 234 |
fmt <- "%s does not appear to refer to a Conda instance of Python; ignoring" |
| 235 |
warningf(fmt, renv_path_pretty(python)) |
| 236 |
break |
| 237 |
} |
| 238 |
|
| 239 |
# use this edition of python without further adieu |
| 240 |
version <- renv_python_version(python) |
| 241 |
return(renv_use_python_fini(info, name, version, project)) |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
# TODO: how do we select which version of python we want to use? |
| 246 |
name <- name %||% renv_python_envpath(project, "conda") |
| 247 |
python <- renv_use_python_condaenv_impl(project, name) |
| 248 |
info <- renv_python_info(python) |
| 249 |
version <- renv_python_version(python) |
| 250 |
|
| 251 |
renv_use_python_fini(info, name, version, project) |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
renv_use_python_fini <- function(info, name, version, project) { |
| 256 |
|
| 257 |
# normalize project path for later comparison |
| 258 |
project <- renv_path_normalize(project) |
| 259 |
|
| 260 |
# handle 'name' -- treat values containing slashes specially, and |
| 261 |
# check if those paths are project-relative environments |
| 262 |
if (!is.null(name) && grepl("/", name, fixed = TRUE)) { |
| 263 |
name <- renv_path_normalize(name) |
| 264 |
if (startsWith(name, project)) { |
| 265 |
base <- substring(name, nchar(project) + 2L) |
| 266 |
name <- if (grepl("^[.][^/]+$", base)) base else file.path(".", base) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
# form the lockfile fields we'll want to write |
| 271 |
fields <- as.list(c(Version = version, Type = info$type, Name = name)) |
| 272 |
|
| 273 |
# update the lockfile |
| 274 |
lockfile <- renv_lockfile_load(project) |
| 275 |
if (!identical(fields, lockfile$Python)) { |
| 276 |
lockfile$Python <- fields |
| 277 |
renv_lockfile_save(lockfile, project) |
| 278 |
} |
| 279 |
|
| 280 |
# re-initialize with these settings |
| 281 |
renv_load_python(project, fields) |
| 282 |
|
| 283 |
# notify user |
| 284 |
if (!renv_tests_running()) { |
| 285 |
if (is.null(info$type)) { |
| 286 |
fmt <- "- Activated Python %s (%s)." |
| 287 |
writef(fmt, version, renv_path_aliased(info$python)) |
| 288 |
} else { |
| 289 |
fmt <- "- Activated Python %s [%s; %s]" |
| 290 |
writef(fmt, version, info$type, renv_path_aliased(name)) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
# report to user |
| 295 |
setwd(project) |
| 296 |
activate(project = project) |
| 297 |
|
| 298 |
invisible(info$python) |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
# return the path to an existing python binary associated with the virtual |
| 303 |
# environment having name 'name' and version 'version', or "" if no such |
| 304 |
# python instance exists |
| 305 |
renv_use_python_virtualenv_impl_existing <- function(project, |
| 306 |
name = NULL, |
| 307 |
version = NULL) |
| 308 |
{ |
| 309 |
# resolve environment path from name |
| 310 |
name <- name %||% renv_python_envpath(project, "virtualenv", version) |
| 311 |
path <- renv_python_virtualenv_path(name) |
| 312 |
if (!file.exists(path)) |
| 313 |
return("") |
| 314 |
|
| 315 |
# check that this appears to have a valid python executable |
| 316 |
info <- catch(renv_python_info(path)) |
| 317 |
if (inherits(info, "error")) { |
| 318 |
warning(info) |
| 319 |
return("") |
| 320 |
} |
| 321 |
|
| 322 |
# validate version and return |
| 323 |
renv_python_virtualenv_validate(path, version) |
| 324 |
} |
| 325 |
|
| 326 |
# Internal helper for activating a Python virtual environment |
| 327 |
# |
| 328 |
# @param project |
| 329 |
# The project directory. |
| 330 |
# |
| 331 |
# @param name |
| 332 |
# The environment name, if any. If unset, it should be constructed |
| 333 |
# based on the Python executable used (note: _not_ the version parameter) |
| 334 |
# |
| 335 |
# @param version |
| 336 |
# The _requested_ version of Python (which may not be the actual version!) |
| 337 |
# This version should be used as a hint for finding an appropriate version |
| 338 |
# of Python, if the environment needs to be re-created. |
| 339 |
# |
| 340 |
# @param python |
| 341 |
# The copy of Python to be used. When unset, an appropriate version of Python |
| 342 |
# should be discovered based on the `version` parameter. |
| 343 |
# |
| 344 |
# @return |
| 345 |
# The path to the Python binary in the associated virtual environment. |
| 346 |
# |
| 347 |
renv_use_python_virtualenv_impl <- function(project, |
| 348 |
name = NULL, |
| 349 |
version = NULL, |
| 350 |
python = NULL) |
| 351 |
{ |
| 352 |
# first, look for an already-existing python installation |
| 353 |
# associated with the requested version of python |
| 354 |
exe <- renv_use_python_virtualenv_impl_existing(project, name, version) |
| 355 |
if (file.exists(exe)) |
| 356 |
return(exe) |
| 357 |
|
| 358 |
# couldn't resolve environment from requested version; try to find |
| 359 |
# a compatible version of python and re-create that environment |
| 360 |
python <- python %||% renv_python_find(version) |
| 361 |
pyversion <- renv_python_version(python) |
| 362 |
name <- name %||% renv_python_envpath(project, "virtualenv", pyversion) |
| 363 |
path <- renv_python_virtualenv_path(name) |
| 364 |
|
| 365 |
# if the environment already exists, but is associated with a different |
| 366 |
# version of Python, prompt the user to re-create that environment |
| 367 |
if (file.exists(path)) { |
| 368 |
exe <- renv_python_virtualenv_validate(path, version) |
| 369 |
if (file.exists(exe)) |
| 370 |
return(exe) |
| 371 |
} |
| 372 |
|
| 373 |
printf("- Creating virtual environment '%s' ... ", basename(name)) |
| 374 |
vpython <- renv_python_virtualenv_create(python, path) |
| 375 |
writef("Done!") |
| 376 |
|
| 377 |
printf("- Updating Python packages ... ") |
| 378 |
renv_python_virtualenv_update(vpython) |
| 379 |
writef("Done!") |
| 380 |
|
| 381 |
renv_python_virtualenv_validate(path, version) |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
renv_use_python_condaenv_impl <- function(project, |
| 386 |
name = NULL, |
| 387 |
version = NULL, |
| 388 |
python = NULL) |
| 389 |
{ |
| 390 |
# if we can't load reticulate, try installing if there is a version |
| 391 |
# recorded in the lockfile |
| 392 |
if (!requireNamespace("reticulate", quietly = TRUE)) { |
| 393 |
|
| 394 |
# retrieve reticulate record |
| 395 |
lockfile <- renv_lockfile_load(project = project) |
| 396 |
records <- renv_lockfile_records(lockfile) |
| 397 |
reticulate <- records[["reticulate"]] |
| 398 |
|
| 399 |
# if we have a reticulate record, then attempt to restore |
| 400 |
if (!is.null(reticulate)) { |
| 401 |
restore(packages = "reticulate", |
| 402 |
prompt = FALSE, |
| 403 |
project = project) |
| 404 |
} else { |
| 405 |
install(packages = "reticulate", |
| 406 |
prompt = FALSE, |
| 407 |
project = project) |
| 408 |
} |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
# try once more to load reticulate |
| 413 |
if (!requireNamespace("reticulate", quietly = TRUE)) |
| 414 |
stopf("use of conda environments requires the 'reticulate' package") |
| 415 |
|
| 416 |
# TODO: how to handle things like a requested Python version here? |
| 417 |
name <- name %||% renv_python_envpath(project, "conda", version) |
| 418 |
renv_python_conda_select(name, version) |
| 419 |
} |
| 420 |
|
| 421 |
renv_python_deactivate <- function(project) { |
| 422 |
|
| 423 |
file <- renv_lockfile_path(project) |
| 424 |
if (!file.exists(file)) |
| 425 |
return(TRUE) |
| 426 |
|
| 427 |
lockfile <- renv_lockfile_read(file) |
| 428 |
if (is.null(lockfile$Python)) |
| 429 |
return(TRUE) |
| 430 |
|
| 431 |
lockfile$Python <- NULL |
| 432 |
renv_lockfile_write(lockfile, file = file) |
| 433 |
writef("- Deactivated Python -- the lockfile has been updated.") |
| 434 |
|
| 435 |
# notify the user about leftover files they may want to clean up |
| 436 |
candidates <- c( |
| 437 |
renv_paths_renv("python", project = project), |
| 438 |
file.path(project, "requirements.txt"), |
| 439 |
file.path(project, "environment.yml") |
| 440 |
) |
| 441 |
|
| 442 |
leftovers <- candidates[file.exists(candidates)] |
| 443 |
if (length(leftovers)) { |
| 444 |
bulletin( |
| 445 |
"The following file(s) may no longer be needed:", |
| 446 |
paste("-", renv_path_pretty(leftovers)), |
| 447 |
"If these are no longer needed, they can be safely deleted." |
| 448 |
) |
| 449 |
} |
| 450 |
|
| 451 |
TRUE |
| 452 |
|
| 453 |
} |