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 activateis a shell function rather than a standalone binary, so your shell profile (~/.bashrc,~/.zshrc, or Windows PowerShell) must be initialized once:# Run once for your shell (use `zsh`, `bash`, `powershell`, or `cmd.exe`), then restart the terminal: conda init zsh- If
condaitself is not on youryet, invoke it via its full path first (e.g.,~/miniconda3/bin/conda init zshon macOS/Linux, or%USERPROFILE%\miniconda3\Scripts\conda.exe init cmd.exeon Windows).
- Windows PowerShell
PSSecurityException(running scripts is disabled on this system):- Even after
conda init powershell, Windows blocksprofile.ps1by default until you allow local signed/authored scripts for your user account:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- Even after
- 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-outputfor real-time streamingstdout).
- Inside CI pipelines, Makefiles, Dockerfiles, or cron jobs where interactive shell activation is fragile, use
- Activating inside a shell script (
bashnon-interactive subshells):- Plain
conda activate my_envinside ascript 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
- Plain
- Legacy
source activate my_env/activate my_envis deprecated:source activate(Unix) and bareactivate(Windows) were deprecated in Conda 4.4+; always useconda activate my_envacross 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 & 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. |
You must log in or register to comment.
