| py_requirements_files | R Documentation |
py_write_requirements() writes the requirements currently tracked by
py_require(). If freeze = TRUE or if the python environment is not
ephemeral, it writes a fully resolved manifest via pip freeze.
py_read_requirements() reads requirements.txt and .python-version, and
applies them with py_require(). By default, entries are added (action = "add").
These are primarily an alternative interface to py_require(), but can
also work with non-ephemeral virtual environments.
py_write_requirements(
packages = "requirements.txt",
python_version = ".python-version",
...,
freeze = NULL,
python = py_exe(),
quiet = FALSE
)
py_read_requirements(
packages = "requirements.txt",
python_version = ".python-version",
...,
action = c("add", "set", "remove", "none")
)
packages |
Path to the package requirements file. Defaults to
|
python_version |
Path to the Python version file. Defaults to
|
... |
Unused; must be empty. |
freeze |
Logical. If |
python |
Path to the Python executable to use. |
quiet |
Logical; if |
action |
How to apply requirements read by |
Invisibly, a list with two named elements:
packagesCharacter vector of package requirements.
python_versionString specifying the Python version.
To get just the return value without writing any files, you can pass
NULL for file paths, like this:
py_write_requirements(NULL, NULL) py_write_requirements(NULL, NULL, freeze = TRUE)
To continue using py_require() locally while keeping a
requirements.txt up-to-date for deployments, you can register
an exit handler in .Rprofile like this:
reg.finalizer(
asNamespace("reticulate"),
function(ns) {
if (
reticulate::py_available() &&
isTRUE(reticulate::py_config()$ephemeral)
) {
reticulate::py_write_requirements(quiet = TRUE)
}
},
onexit = TRUE
)
This approach is only recommended if you are using git.
Alternatively, you can transition away from using ephemeral python
environemnts via py_require() to using a persistent local virtual
environment you manage. You can create a local virtual environment from
requirements.txt and .python-version using virtualenv_create():
# Note: '.venv' in the current directory is auto-discovered by reticulate.
# https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery
virtualenv_create(
"./.venv",
version = readLines(".python-version"),
requirements = "requirements.txt"
)
If you run into issues, be aware that requirements.txt and
.python-version may not contain all the information necessary to
reproduce the Python environment if the R code sets environment variables
like UV_INDEX or UV_CONSTRAINT.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.