-
Notifications
You must be signed in to change notification settings - Fork 326
/
conda.R
573 lines (477 loc) · 17 KB
/
conda.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#' Interface to conda
#'
#' R functions for managing Python [conda
#' environments](https://conda.io/docs/user-guide/tasks/manage-environments.html).
#'
#' @param envname The name of, or path to, a conda environment.
#'
#' @param conda The path to a `conda` executable. Use `"auto"` to allow
#' `reticulate` to automatically find an appropriate `conda` binary. See
#' **Finding Conda** for more details.
#'
#' @param packages A character vector, indicating package names which should be
#' installed or removed. Use `python=<version>` to request the installation
#' of a specific version of Python.
#'
#' @param pip Boolean; use `pip` when downloading or installing packages?
#' Defaults to `FALSE`.
#'
#' @param ... Optional arguments, reserved for future expansion.
#'
#' @return `conda_list()` returns a data frame with the names and paths to the
#' respective python binaries of available environments. `conda_create()`
#' returns the path to the python binary of the created environment.
#' `conda_binary()` returns the location of the main conda binary or `NULL`
#' if none can be found.
#'
#' @name conda-tools
#'
#' @importFrom jsonlite fromJSON
#'
#' @section Finding Conda:
#'
#' When `conda = "auto"`, `reticulate` will attempt to automatically find an
#' Anaconda / Miniconda installation and use that. `reticulate` will search the
#' following locations:
#'
#' 1. The location specified by the `reticulate.conda_binary` \R option;
#' 2. The [miniconda_path()] location (if it exists);
#' 3. The program `PATH`;
#' 4. A set of pre-defined locations where Conda is typically installed.
#'
#' @export
conda_list <- function(conda = "auto") {
# resolve conda binary
conda <- conda_binary(conda)
# list envs -- discard stderr as Anaconda may emit warnings that can
# otherwise be ignored; see e.g. https://github.com/rstudio/reticulate/issues/474
conda_envs <- suppressWarnings(
system2(conda, args = c("info", "--json"), stdout = TRUE, stderr = FALSE)
)
# check for error
status <- attr(conda_envs, "status")
if (!is.null(status)) {
# show warning if conda_diagnostics are enabled
if (getOption("reticulate.conda_diagnostics", default = FALSE)) {
errmsg <- attr(status, "errmsg")
warning("Error ", status, " occurred running ", conda, " ", errmsg)
}
# return empty data frame
return(data.frame(
name = character(),
python = character(),
stringsAsFactors = FALSE)
)
}
# strip out anaconda cloud prefix (not valid json)
if (length(conda_envs) > 0 && grepl("Anaconda Cloud", conda_envs[[1]], fixed = TRUE))
conda_envs <- conda_envs[-1]
# parse conda info
info <- fromJSON(conda_envs)
# convert to json
conda_envs <- info$envs
conda_envs <- Filter(file.exists, conda_envs)
# normalize and remove duplicates (seems necessary on Windows as Anaconda
# may report both short-path and long-path versions of the same environment)
conda_envs <- unique(normalizePath(conda_envs))
# return an empty data.frame when no envs are found
if (length(conda_envs) == 0L) {
return(data.frame(
name = character(),
python = character(),
stringsAsFactors = FALSE)
)
}
# build data frame
name <- character()
python <- character()
for (conda_env in conda_envs) {
name <- c(name, basename(conda_env))
conda_env_dir <- conda_env
if (!is_windows())
conda_env_dir <- file.path(conda_env_dir, "bin")
conda_env_python <- file.path(conda_env_dir, "python")
if (is_windows()) {
conda_env_python <- paste0(conda_env_python, ".exe")
conda_env_python <- normalizePath(conda_env_python)
}
python <- c(python, conda_env_python)
}
data.frame(name = name, python = python, stringsAsFactors = FALSE)
}
#' @param forge Boolean; include the [Conda Forge](https://conda-forge.org/)
#' repository?
#'
#' @param channel An optional character vector of Conda channels to include.
#' When specified, the `forge` argument is ignored. If you need to
#' specify multiple channels, including the Conda Forge, you can use
#' `c("conda-forge", <other channels>)`.
#'
#' @param python_version The version of Python to be used in this Conda
#' environment. The associated Python package from Conda will be requested
#' as `python={python_version}`. When `NULL`, the default `python` package
#' will be used instead. For example, use `python_version = "3.6"` to request
#' that the Conda environment be created with a copy of Python 3.6. This
#' argument will be ignored if `python` is specified as part of the `packages`
#' argument, for backwards compatibility.
#'
#' @rdname conda-tools
#'
#' @keywords internal
#'
#' @export
conda_create <- function(envname = NULL,
packages = NULL,
forge = TRUE,
channel = character(),
conda = "auto",
python_version = NULL)
{
# resolve conda binary
conda <- conda_binary(conda)
# resolve environment name
envname <- condaenv_resolve(envname)
# resolve packages argument
if (!any(grepl("^python", packages))) {
python_package <- if (is.null(python_version))
"python"
else
sprintf("python=%s", python_version)
packages <- c(python_package, packages)
}
# create the environment
args <- conda_args("create", envname, packages)
# add user-requested channels
channels <- if (length(channel))
channel
else if (forge)
"conda-forge"
for (ch in channels)
args <- c(args, "-c", ch)
result <- system2(conda, shQuote(args))
if (result != 0L) {
stop("Error ", result, " occurred creating conda environment ", envname,
call. = FALSE)
}
# return the path to the python binary
conda_python(envname = envname, conda = conda)
}
#' @rdname conda-tools
#' @export
conda_remove <- function(envname, packages = NULL, conda = "auto") {
# resolve conda binary
conda <- conda_binary(conda)
# resolve environment name
envname <- condaenv_resolve(envname)
# no packages means everything
if (is.null(packages))
packages <- "--all"
# remove packges (or the entire environment)
args <- conda_args("remove", envname, packages)
result <- system2(conda, shQuote(args))
if (result != 0L) {
stop("Error ", result, " occurred removing conda environment ", envname,
call. = FALSE)
}
}
#' @param forge Boolean; include the [Conda Forge](https://conda-forge.org/)
#' repository?
#'
#' @param channel An optional character vector of Conda channels to include.
#' When specified, the `forge` argument is ignored. If you need to
#' specify multiple channels, including the Conda Forge, you can use
#' `c("conda-forge", <other channels>)`.
#'
#' @param pip_ignore_installed Ignore installed versions when using pip
#' (defaults to `FALSE`). Set this to `TRUE` so that specific package versions
#' can be installed even if they are downgrades. The `FALSE` option is useful
#' for situations where you don't want a pip install to attempt an overwrite of
#' a conda binary package (e.g. SciPy on Windows which is very difficult to
#' install via pip due to compilation requirements).
#'
#' @param pip_options An optional character vector of additional command line
#' arguments to be passed to `pip`. Only relevant when `pip = TRUE`.
#'
#' @rdname conda-tools
#'
#' @keywords internal
#'
#' @export
conda_install <- function(envname = NULL,
packages,
forge = TRUE,
channel = character(),
pip = FALSE,
pip_options = character(),
pip_ignore_installed = FALSE,
conda = "auto",
python_version = NULL,
...)
{
check_forbidden_install("Python packages")
# resolve conda binary
conda <- conda_binary(conda)
# resolve environment name
envname <- condaenv_resolve(envname)
# honor request for specific version of Python package
python_package <- if (is.null(python_version))
NULL
else if (grepl("[><=]", python_version))
paste0("python", python_version)
else
sprintf("python=%s", python_version)
# check to see if we already have a valid Python installation for
# this conda environment
python <- tryCatch(
conda_python(envname = envname, conda = conda),
error = identity
)
# if this conda environment doesn't seem to exist, auto-create it
if (inherits(python, "error") || !file.exists(python)) {
conda_create(
envname = envname,
packages = python_package %||% "python",
forge = forge,
channel = channel,
conda = conda
)
python <- conda_python(envname = envname, conda = conda)
}
# if the user has requested a specific version of Python, ensure that
# version of Python is installed into the requested environment
# (should be no-op if that copy of Python already installed)
if (!is.null(python_version)) {
args <- conda_args("install", envname, python_package)
status <- system2(conda, shQuote(args))
if (status != 0L) {
fmt <- "installation of '%s' into environment '%s' failed [error code %i]"
msg <- sprintf(fmt, python_package, envname, status)
stop(msg, call. = FALSE)
}
}
# delegate to pip if requested
if (pip) {
result <- pip_install(
python = python,
packages = packages,
pip_options = pip_options,
ignore_installed = pip_ignore_installed
)
return(result)
}
# otherwise, use conda
args <- conda_args("install", envname)
# add user-requested channels
channels <- if (length(channel))
channel
else if (forge)
"conda-forge"
for (ch in channels)
args <- c(args, "-c", ch)
args <- c(args, python_package, packages)
result <- system2(conda, shQuote(args))
# check for errors
if (result != 0L) {
fmt <- "one or more Python packages failed to install [error code %i]"
stopf(fmt, result)
}
invisible(packages)
}
#' @rdname conda-tools
#' @export
conda_binary <- function(conda = "auto") {
# automatic lookup if requested
if (identical(conda, "auto")) {
conda <- find_conda()
if (is.null(conda))
stop("Unable to find conda binary. Is Anaconda installed?", call. = FALSE)
conda <- conda[[1]]
}
conda <- normalizePath(conda, winslash = "/", mustWork = FALSE)
# if the user has requested a conda binary in the 'condabin' folder,
# try to find and use its sibling in the 'bin' folder instead as
# we rely on other tools typically bundled in the 'bin' folder
# https://github.com/rstudio/keras/issues/691
if (!is_windows()) {
altpath <- file.path(dirname(conda), "../bin/conda")
if (file.exists(altpath))
return(normalizePath(altpath, winslash = "/", mustWork = TRUE))
} else {
# on Windows it's preferable to conda.bat located in the 'condabin'
# folder. if the user passed the path to a 'Scripts/conda.exe' we will
# try to find the 'conda.bat'.
altpath <- file.path(dirname(conda), "../condabin/conda.bat")
if (file.exists(altpath))
return(normalizePath(altpath, winslash = "/", mustWork = TRUE))
}
# validate existence
if (!file.exists(conda))
stop("Specified conda binary '", conda, "' does not exist.", call. = FALSE)
# return conda
conda
}
#' @rdname conda-tools
#' @export
conda_version <- function(conda = "auto") {
conda_bin <- conda_binary(conda)
system2(conda_bin, "--version", stdout = TRUE)
}
#' @rdname conda-tools
#' @export
conda_python <- function(envname = NULL, conda = "auto") {
# resolve envname
envname <- condaenv_resolve(envname)
# for fully-qualified paths, construct path explicitly
if (grepl("[/\\\\]", envname)) {
suffix <- if (is_windows()) "python.exe" else "bin/python"
path <- file.path(envname, suffix)
if (file.exists(path))
return(path)
fmt <- "no conda environment exists at path '%s'"
stop(sprintf(fmt, envname))
}
# otherwise, list conda environments and try to find it
conda_envs <- conda_list(conda = conda)
env <- subset(conda_envs, conda_envs$name == envname)
if (nrow(env) > 0)
path.expand(env$python[[1]])
else
stop("conda environment ", envname, " not found")
}
find_conda <- function() {
# allow specification of conda executable
conda <- getOption("reticulate.conda_binary")
if (!is.null(conda))
return(conda)
# if miniconda is installed, use it
if (miniconda_exists())
return(miniconda_conda())
# if there is a conda executable on the PATH, use it
conda <- Sys.which("conda")
if (nzchar(conda))
return(conda)
# otherwise, search common locations for conda
# TODO: these won't work on Windows as conda is often installed
# at e.g. 'condabin/conda.bat' or similar!
conda_locations <- c(
miniconda_conda(),
path.expand("~/opt/anaconda/bin/conda"),
path.expand("~/opt/anaconda2/bin/conda"),
path.expand("~/opt/anaconda3/bin/conda"),
path.expand("~/opt/anaconda4/bin/conda"),
path.expand("~/opt/miniconda/bin/conda"),
path.expand("~/opt/miniconda2/bin/conda"),
path.expand("~/opt/miniconda3/bin/conda"),
path.expand("~/opt/miniconda4/bin/conda"),
path.expand("~/anaconda/bin/conda"),
path.expand("~/anaconda2/bin/conda"),
path.expand("~/anaconda3/bin/conda"),
path.expand("~/anaconda4/bin/conda"),
path.expand("~/miniconda/bin/conda"),
path.expand("~/miniconda2/bin/conda"),
path.expand("~/miniconda3/bin/conda"),
path.expand("~/miniconda4/bin/conda"),
path.expand("/opt/anaconda/bin/conda"),
path.expand("/opt/anaconda2/bin/conda"),
path.expand("/opt/anaconda3/bin/conda"),
path.expand("/opt/anaconda4/bin/conda"),
path.expand("/opt/miniconda/bin/conda"),
path.expand("/opt/miniconda2/bin/conda"),
path.expand("/opt/miniconda3/bin/conda"),
path.expand("/opt/miniconda4/bin/conda"),
path.expand("/anaconda/bin/conda"),
path.expand("/anaconda2/bin/conda"),
path.expand("/anaconda3/bin/conda"),
path.expand("/anaconda4/bin/conda"),
path.expand("/miniconda/bin/conda"),
path.expand("/miniconda2/bin/conda"),
path.expand("/miniconda3/bin/conda"),
path.expand("/miniconda4/bin/conda")
)
# on Windows, check the registry for a compatible version of Anaconda
if (is_windows()) {
anaconda_versions <- windows_registry_anaconda_versions()
anaconda_versions <- subset(anaconda_versions, anaconda_versions$arch == .Platform$r_arch)
if (nrow(anaconda_versions) > 0) {
conda_scripts <- utils::shortPathName(
file.path(anaconda_versions$install_path, "Scripts", "conda.exe")
)
conda_bats <- utils::shortPathName(
file.path(anaconda_versions$install_path, "condabin", "conda.bat")
)
conda_locations <- c(conda_locations, conda_bats, conda_scripts)
}
}
# keep only conda locations that exist
conda_locations <- conda_locations[file.exists(conda_locations)]
if (length(conda_locations))
return(conda_locations)
# explicitly return NULL when no conda found
NULL
}
condaenv_resolve <- function(envname = NULL) {
python_environment_resolve(
envname = envname,
resolve = identity
)
}
condaenv_exists <- function(envname = NULL, conda = "auto") {
# check that conda is installed
condabin <- tryCatch(conda_binary(conda = conda), error = identity)
if (inherits(condabin, "error"))
return(FALSE)
# check that the environment exists
python <- tryCatch(conda_python(envname, conda = conda), error = identity)
if (inherits(python, "error"))
return(FALSE)
# validate the Python binary exists
file.exists(python)
}
conda_args <- function(action, envname = NULL, ...) {
envname <- condaenv_resolve(envname)
# use '--prefix' as opposed to '--name' if envname looks like a path
args <- c(action, "--yes")
if (grepl("[/\\]", envname))
args <- c(args, "--prefix", envname, ...)
else
args <- c(args, "--name", envname, ...)
args
}
is_condaenv <- function(dir) {
file.exists(file.path(dir, "conda-meta"))
}
conda_list_packages <- function(envname = NULL, conda = "auto", no_pip = TRUE) {
conda <- conda_binary(conda)
envname <- condaenv_resolve(envname)
# create the environment
args <- c("list")
if (grepl("[/\\]", envname)) {
args <- c(args, "--prefix", envname)
} else {
args <- c(args, "--name", envname)
}
if (no_pip)
args <- c(args, "--no-pip")
args <- c(args, "--json")
output <- system2(conda, shQuote(args), stdout = TRUE)
status <- attr(output, "status") %||% 0L
if (status != 0L) {
fmt <- "error listing conda environment [status code %i]"
stopf(fmt, status)
}
parsed <- jsonlite::fromJSON(output)
data.frame(
package = parsed$name,
version = parsed$version,
requirement = paste(parsed$name, parsed$version, sep = "="),
channel = parsed$channel,
stringsAsFactors = FALSE
)
}
conda_installed <- function() {
condabin <- tryCatch(conda_binary(), error = identity)
if (inherits(condabin, "error"))
return(FALSE)
else
return(TRUE)
}