-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathpython.Rmd
More file actions
84 lines (59 loc) · 2.83 KB
/
python.Rmd
File metadata and controls
84 lines (59 loc) · 2.83 KB
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
---
title: "Using Python with renv"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Using Python with renv}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = FALSE
)
```
If you're using renv with an R project that also depends on some Python
packages (say, through the [reticulate](https://rstudio.github.io/reticulate/)
package), then you may find renv's Python integration useful.
## Activating Python integration
Python integration can be activated on a project-by-project basis. Use
`renv::use_python()` to tell renv to create and use a project-local Python
environment with your project. If the reticulate package is installed and
active, then renv will use the same version of Python that reticulate
normally would when generating the virtual environment. Alternatively, you can
set the `RETICULATE_PYTHON` environment variable to instruct renv to use a
different version of Python.
If you'd rather tell renv to use an existing Python virtual environment, you
can do so by passing the path of that virtual environment instead -- use
`renv::use_python(python = "/path/to/python")` and renv will record and
use that Python interpreter with your project. This can also be used with
pre-existing virtual environments and Conda environments.
## Deactivating Python integration
If you'd like to deactivate Python integration for a project, you can do so
with:
```{r}
renv::use_python(python = FALSE)
```
This removes the `Python` section from `renv.lock`. Note that this does not
remove any previously-created virtual environments or `requirements.txt` files;
those can be deleted manually if no longer needed.
## Understanding Python integration
Once Python integration is active, renv will attempt to manage the state of
your Python virtual environment when `snapshot()` / `restore()` is called. With
this, projects that use renv and Python can ensure that Python dependencies
are tracked in addition to R package dependencies. Note that future restores
will require both `renv.lock` (for R package dependencies) and
`requirements.txt` (for Python package dependencies).
### Virtual environments
When using virtual environments, the following extensions are provided:
- `renv::snapshot()` calls `pip freeze > requirements.txt` to save the
set of installed Python packages;
- `renv::restore()` calls `pip install -r requirements.txt` to install
the previously-recorded set of Python packages.
### Conda environments
When using Conda environments, the following extensions are provided:
- `renv::snapshot()` calls `conda env export > environment.yml` to save the
set of installed Python packages;
- `renv::restore()` calls `conda env [create/update] --file environment.yml` to
install the previously-recorded set of Python packages.