Skip to contents

Associate a version of Python with your lesson. This is essentially a wrapper around renv::use_python().

To add Python packages, py_install() is provided, which installs Python packages with reticulate::py_install() and then records them in the renv environment. This ensures manage_deps() keeps track of the Python packages as well.

Usage

use_python(
  path = ".",
  python = NULL,
  type = c("auto", "virtualenv", "conda", "system"),
  ...
)

py_install(packages, path = ".", ...)

Arguments

path

path to your lesson. Defaults to the current working directory.

python

The path to the version of Python to be used with this project. See Finding Python for more details.

type

The type of Python environment to use. When "auto" (the default), virtual environments will be used.

...

Further arguments to be passed to reticulate::py_install()

packages

Python packages to be installed as a character vecto.

Value

The path to the Python executable. Note that this function is mainly called for its side effects.

Details

This helper function adds Python as a dependency to the renv lockfile and installs a Python environment of the specified type. This ensures any Python packages used for this lesson are installed separately from the user's main library, much like the R packages (see manage_deps()).

Note that renv is not (yet) able to automatically detect Python package dependencies (e.g. from import statements). So any required Python packages still need to be installed manually. To facilitate this, the py_install() helper is provided. This will install Python packages in the correct environment and record them in a requirements.txt file, which will be tracked by renv. Subsequent calls of manage_deps() will then correctly restore the required Python packages if needed.

See also

renv::use_python(), py_install()

Examples

if (FALSE) {
tmp <- tempfile()
on.exit(unlink(tmp))

## Create lesson with Python support
lsn <- create_lesson(tmp, name = "This Lesson", open = FALSE, add_python = TRUE)
lsn

## Add Python as a dependency to an existing lesson
setwd(lsn)
use_python()

## Install Python packages and record them as dependencies
py_install("numpy")
}