Set up a python environment with required packages

setup_py_env(
  py_pkgs = c("scipy", "pandas"),
  python_version = NULL,
  py_env = c("conda", "virtual"),
  env_name = PYTHON_R_ENV,
  conda_env = get_conda_envs(),
  conda_path = NULL,
  update = FALSE,
  pip = FALSE,
  required = TRUE
)

install_py_pkgs(
  py_pkgs = c("scipy", "pandas"),
  py_env = c("conda", "virtual"),
  conda_path = NULL,
  env_name = PYTHON_R_ENV,
  pip = FALSE,
  update = FALSE,
  python_version = NULL
)

get_conda_paths()

Arguments

py_pkgs

vector of python packages to install

python_version

The requested Python version. Ignored when attempting to install with a Python virtual environment.

py_env

Environment type. Either a conda or virtual environment.

env_name

an environment name to use for new virtual or conda environments.

conda_env

path of the conda environment to use if py_env = "conda". Run reticulate::conda_list() to see a list of available conda environments. The conda_env should match the python element of that list if you are loading an existing conda environment. If conda_env is not found in this list, a new one will be created matching the name of env_name.

conda_path

The path to a conda executable. If NULL, will default to the first one found. See get_conda_paths() for details.

update

Logical (TRUE/FALSE). If TRUE, update packages that are already installed. Otherwise skip their installation.

pip

Logical (TRUE/FALSE). Use pip for package installation? This is only relevant when Conda environments are used, as otherwise packages will be installed from the Conda repositories.

required

Logical (TRUE/FALSE). Is the requested copy of Python required? If TRUE, an error will be emitted if the requested copy of Python does not exist. If FALSE, the request is taken as a hint only, and scanning for other versions will still proceed. See reticulate::use_condaenv() for more details.

Value

a named list of specifications pertaining to the python environment, including the packages that are installed there.

Details

An environment will be required for each new R session.

For a virtual environment, the user would be installing these packages every time they want to run python on a clean R session. It is a smaller package size that a conda environment.

For a conda environment, users only need to install the required packages once. This may be less ideal if many python modules are required, or you are developing an R package.

see ?reticulate::py_install for more details

Inspired from reticulate:::find_conda(), though this function will return all paths instead of the first one found in a hierarchy of priorities

Functions

  • install_py_pkgs(): Install python packages to a python environment

  • get_conda_paths(): Search for conda installations

Note

You must restart your R session if you want to alternate between both environment types

See also

Examples

if (FALSE) {

## With a conda environment (the default) ##
py_env <- setup_py_env(py_pkgs = c("pandas", "numpy", "scipy"))
py_env

# Specify a specific conda environment and conda installation
conda_envs <- reticulate::conda_list()
conda_paths <- pythonR::get_conda_paths()

py_env <- setup_py_env(
  py_pkgs = c("pandas", "numpy", "scipy", "tiktoken"),
  conda_path = conda_paths[1],
  conda_env = conda_envs$python[1],
  required = TRUE,
  update = TRUE,
  pip = TRUE
)


# Installing a package after setup (works with both environment types)
py_env <- setup_py_env(py_pkgs = c("pandas", "numpy"), py_env = "conda")
install_py_pkgs(py_pkgs = c('scipy'), env_name = py_env$env_name)

# Check that installed modules can be imported
check_py_pkgs_installed(c("pandas", "numpy", "scipy"))


## With a virtual environment ##
py_env <- setup_py_env(py_pkgs = c("pandas", "numpy", "scipy"), py_env = "virtual")
py_env

# shutdown virtual environment
shutdown_virtual_env(py_env$env_name)

}