| 1 |
--- |
| 2 |
title: "Using Python with renv" |
| 3 |
output: rmarkdown::html_vignette |
| 4 |
vignette: > |
| 5 |
%\VignetteIndexEntry{Using Python with renv} |
| 6 |
%\VignetteEngine{knitr::rmarkdown} |
| 7 |
%\VignetteEncoding{UTF-8} |
| 8 |
--- |
| 9 |
|
| 10 |
```{r, include=FALSE} |
| 11 |
knitr::opts_chunk$set( |
| 12 |
collapse = TRUE, |
| 13 |
comment = "#>", |
| 14 |
eval = FALSE |
| 15 |
) |
| 16 |
``` |
| 17 |
|
| 18 |
If you're using renv with an R project that also depends on some Python |
| 19 |
packages (say, through the [reticulate](https://rstudio.github.io/reticulate/) |
| 20 |
package), then you may find renv's Python integration useful. |
| 21 |
|
| 22 |
|
| 23 |
## Activating Python integration |
| 24 |
|
| 25 |
Python integration can be activated on a project-by-project basis. Use |
| 26 |
`renv::use_python()` to tell renv to create and use a project-local Python |
| 27 |
environment with your project. If the reticulate package is installed and |
| 28 |
active, then renv will use the same version of Python that reticulate |
| 29 |
normally would when generating the virtual environment. Alternatively, you can |
| 30 |
set the `RETICULATE_PYTHON` environment variable to instruct renv to use a |
| 31 |
different version of Python. |
| 32 |
|
| 33 |
If you'd rather tell renv to use an existing Python virtual environment, you |
| 34 |
can do so by passing the path of that virtual environment instead -- use |
| 35 |
`renv::use_python(python = "/path/to/python")` and renv will record and |
| 36 |
use that Python interpreter with your project. This can also be used with |
| 37 |
pre-existing virtual environments and Conda environments. |
| 38 |
|
| 39 |
|
| 40 |
## Deactivating Python integration |
| 41 |
|
| 42 |
If you'd like to deactivate Python integration for a project, you can do so |
| 43 |
with: |
| 44 |
|
| 45 |
```{r} |
| 46 |
renv::use_python(python = FALSE) |
| 47 |
``` |
| 48 |
|
| 49 |
This removes the `Python` section from `renv.lock`. Note that this does not |
| 50 |
remove any previously-created virtual environments or `requirements.txt` files; |
| 51 |
those can be deleted manually if no longer needed. |
| 52 |
|
| 53 |
|
| 54 |
## Understanding Python integration |
| 55 |
|
| 56 |
Once Python integration is active, renv will attempt to manage the state of |
| 57 |
your Python virtual environment when `snapshot()` / `restore()` is called. With |
| 58 |
this, projects that use renv and Python can ensure that Python dependencies |
| 59 |
are tracked in addition to R package dependencies. Note that future restores |
| 60 |
will require both `renv.lock` (for R package dependencies) and |
| 61 |
`requirements.txt` (for Python package dependencies). |
| 62 |
|
| 63 |
|
| 64 |
### Virtual environments |
| 65 |
|
| 66 |
When using virtual environments, the following extensions are provided: |
| 67 |
|
| 68 |
- `renv::snapshot()` calls `pip freeze > requirements.txt` to save the |
| 69 |
set of installed Python packages; |
| 70 |
|
| 71 |
- `renv::restore()` calls `pip install -r requirements.txt` to install |
| 72 |
the previously-recorded set of Python packages. |
| 73 |
|
| 74 |
|
| 75 |
### Conda environments |
| 76 |
|
| 77 |
When using Conda environments, the following extensions are provided: |
| 78 |
|
| 79 |
- `renv::snapshot()` calls `conda env export > environment.yml` to save the |
| 80 |
set of installed Python packages; |
| 81 |
|
| 82 |
- `renv::restore()` calls `conda env [create/update] --file environment.yml` to |
| 83 |
install the previously-recorded set of Python packages. |
| 84 |
|