How do you activate a Conda environment from the terminal or command prompt? | A1Lab@MZeeshanM to PythonEnglish • 2 days ago

How do you activate a Conda environment from the terminal or command prompt?

message-square
0

Run conda activate my_env directly in your terminal or Anaconda Prompt (replacing my_env with your environment name or filesystem path), after running conda init once per shell if your terminal reports CommandNotFoundError: Your shell has not been properly configured to use 'conda activate' (or on Windows PowerShell if execution policy blocks profile scripts, run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser first).

Basic Example

# 1. List available Conda environments to verify the exact environment name:
conda env list

# 2. Recommended: Activate an environment by name (works in macOS/Linux bash/zsh & Windows Anaconda Prompt):
conda activate ml_env

# Verify the active interpreter and environment path:
which python        # macOS / Linux -> .../miniconda3/envs/ml_env/bin/python
where python        # Windows CMD   -> ...\miniconda3\envs\ml_env\python.exe

# 3. Activate an environment created with `--prefix` (`-p`) via its directory path:
conda activate ./envs/project_env

# 4. Deactivate the current environment and return to `(base)`:
conda deactivate

Key Details & Edge Cases

  • Fixing CommandNotFoundError: Your shell has not been properly configured to use 'conda activate':
    • conda activate is a shell function rather than a standalone binary, so your shell profile (~/.bashrc, ~/.zshrc, or Windows PowerShell $PROFILE) must be initialized once:
      # Run once for your shell (use `zsh`, `bash`, `powershell`, or `cmd.exe`), then restart the terminal:
      conda init zsh
      
    • If conda itself is not on your $PATH yet, invoke it via its full path first (e.g., ~/miniconda3/bin/conda init zsh on macOS/Linux, or %USERPROFILE%\miniconda3\Scripts\conda.exe init cmd.exe on Windows).
  • Windows PowerShell PSSecurityException (running scripts is disabled on this system):
    • Even after conda init powershell, Windows blocks profile.ps1 by default until you allow local signed/authored scripts for your user account:
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      
  • Running a single command inside an environment without activating it (conda run):
    • Inside CI pipelines, Makefiles, Dockerfiles, or cron jobs where interactive shell activation is fragile, use conda run -n ml_env python script.py (add --no-capture-output for real-time streaming stdout).
  • Activating inside a shell script (bash non-interactive subshells):
    • Plain conda activate my_env inside a #!/bin/bash script fails because non-interactive shells do not source ~/.bashrc. Initialize the shell hook explicitly inside the script first:
      #!/usr/bin/env bash
      eval "$(conda shell.bash hook)"
      conda activate ml_env
      python train.py
      
  • Legacy source activate my_env / activate my_env is deprecated:
    • source activate (Unix) and bare activate (Windows) were deprecated in Conda 4.4+; always use conda activate my_env across every platform.

Comparison of Conda Environment Activation & Execution Commands

Command Supported Shells / Context Modifies Current Terminal Session? Requires conda init? Best Used When
conda activate my_env (Recommended) bash, zsh, fish, Windows CMD, PowerShell, Anaconda Prompt Yes (updates $PATH & prompt prefix (my_env)) Yes (once per shell) Interactive terminal development, running python, pip, pytest, or Jupyter.
conda activate /path/to/env All initialized shells Yes Yes Activating project-local environments created with conda create --prefix ./env.
conda run -n my_env <cmd> Any shell, CI runners, cron jobs, Dockerfiles No (runs subprocess in target env) No Executing scripts in automated pipelines without interactive shell activation.
eval "$(conda shell.bash hook)" && conda activate my_env Non-interactive .sh shell scripts Yes (within that script subshell) No Writing reproducible Bash automation scripts that switch Conda environments.