LinuxCommandLibrary

conda-activate

Activate a Conda environment

TLDR

Activate an existing environment named myenv

$ conda activate myenv
copy

Activate an existing environment located at custom path
$ conda activate [path/to/myenv]
copy

Stack myenv environment on top of a previous environment making libraries/commands/variables from both accessible
$ conda activate --stack myenv
copy

Start a clean environment myenv without stacking it making previous environment libraries/commands/variables not accessible
$ conda activate --no-stack myenv
copy

Display help
$ conda activate [[-h|--help]]
copy

SYNOPSIS

conda activate [ENVIRONMENT_NAME_OR_PATH] [OPTIONS]

PARAMETERS

ENVIRONMENT_NAME_OR_PATH
    The name of the Conda environment to activate (e.g., 'my_env'), or the full path to the environment's directory. This is a positional argument.

-h, --help
    Show a help message for the activate command and exit.

-s, --stack
    Stack the specified environment on top of the currently active environment, rather than replacing it. This means the new environment's packages will be preferred, but the previous environment remains partially active in the PATH.

-p, --prefix
    Specify the full path to the environment's prefix directory. This explicitly indicates that the argument provided is a path, not just an environment name.

DESCRIPTION

The conda activate command is fundamental for managing isolated Python and other software environments created with Conda. When executed, it modifies the shell's environment variables, most notably the PATH, to point to the executables and libraries of the specified Conda environment. This ensures that when you run commands like python or pip, you are interacting with the versions and packages installed within that specific environment, rather than the system's default or another Conda environment.

This isolation is crucial for reproducible research, development, and data science projects, as it prevents dependency conflicts between different projects. For example, Project A might require Python 3.8 and a specific version of NumPy, while Project B needs Python 3.10 and a different NumPy version. conda activate allows seamless switching between these isolated setups without interfering with each other.

CAVEATS

  • Shell Initialization Required: For conda activate to work, Conda must be initialized in your shell using conda init. This command sets up necessary shell functions. Without it, conda activate will likely fail or require manual sourcing of scripts.
  • Not for Non-Interactive Scripts: Directly using conda activate in non-interactive scripts (e.g., a Bash script that is not sourced) is generally not recommended as it modifies the shell's state. For such cases, consider conda run or `conda shell activate` (if available and appropriate for your Conda version) to execute commands within a specific environment.
  • Base Environment: If no environment is specified, conda activate often defaults to activating the 'base' environment.
  • Path Management: Be aware that conda activate prepends environment-specific paths to your PATH variable. Mismanagement can lead to unexpected command execution if not careful.

HOW IT WORKS

Unlike simple path modification, conda activate relies on shell functions (set up by conda init) that introspect the current shell and modify its environment. When you run conda activate my_env, the shell function `conda_activate` is executed. This function performs several actions, including:
1. Updating the PATH environment variable to prioritize the executables within my_env/bin (or `Scripts` on Windows).
2. Setting other environment variables specific to the activated environment (e.g., `CONDA_PREFIX`, `CONDA_DEFAULT_ENV`).
3. Potentially running activation scripts defined within the environment itself (e.g., in `conda-meta/activate.d/`).

DEACTIVATING ENVIRONMENTS

To revert to the previous environment or the base environment, use the command conda deactivate. This command restores the shell's environment variables to their state before the last activation, effectively exiting the current Conda environment.

HISTORY

Conda was developed by Continuum Analytics (now Anaconda Inc.) to address the growing challenges of package and environment management, particularly in the scientific computing and data science communities. Before Conda, managing complex dependencies across different projects often led to 'dependency hell.' The introduction of conda activate, as a core component of the Conda ecosystem, provided a robust, cross-platform, and user-friendly mechanism to switch between isolated software environments. Its development has focused on improving shell integration, performance, and reliability, evolving with various shell types (Bash, Zsh, Fish, PowerShell) to ensure a consistent user experience.

SEE ALSO

conda deactivate(1), conda create(1), conda env list(1), conda init(1), conda run(1), source(1), export(1)

Copied to clipboard