How to activate a Conda environment

Running a project from the wrong Conda environment can make Python import packages from a different prefix than the one you prepared. Activating the environment changes the current shell so commands resolve the selected environment's executables and activation scripts before you run Python, pip, or Conda package commands.

conda activate accepts an environment name or a directory path. A named environment such as analytics resolves under Conda's environment directories, while a prefix environment is activated with its path, such as ./envs.

Activation is local to the terminal session. If the command is not recognized or asks for shell setup, initialize Conda for that shell and open a new terminal before retrying; adding Anaconda to PATH by itself does not run package activation scripts.

Steps to activate a Conda environment:

  1. Open a terminal where Conda activation is initialized.

    If conda activate returns a shell initialization error, initialize Conda for that shell and reopen the terminal.
    Related: How to initialize Conda for a shell

  2. Activate the target environment by name.
    $ conda activate analytics
    (analytics) $

    Replace analytics with the environment name. For a prefix environment, use the path instead, such as conda activate ./envs.

  3. Confirm that the active marker moved to the target environment.
    $ conda info --envs
    
    # conda environments:
    #
    # * -> active
    # + -> frozen
    base                     /opt/conda
    analytics            *   /opt/conda/envs/analytics

    The asterisk marks the active environment. Newer Conda output may also show a plus sign for frozen environments.

  4. Check the active environment prefix.
    $ echo $CONDA_PREFIX
    /opt/conda/envs/analytics

    The prefix should point to the selected environment, not to the base installation.

  5. Run Python from the activated environment.
    $ python -c "import sys; print(sys.executable)"
    /opt/conda/envs/analytics/bin/python

    Package commands run after activation use the active environment unless you pass an explicit environment option such as --name or --prefix.