[QUESTION] Conda environment workflow guidance needed - inherited environment but missing shell functions

Status Fixed / completed
Maintainer reply ✓ Yes — catherinewu
Activity 5 comments · opened Jun 24, 2025 · closed Dec 2, 2025
💡 Likely answer: A maintainer (catherinewu, contributor) responded on this thread — see the highlighted reply below.

Environment

  • Platform: Anthropic API
  • Claude CLI version: Latest
  • Operating System: Windows + VS Code SSH + Docker Desktop Ubuntu container
  • Terminal: bash
  • Conda version: 25.3.1

Background

We're seeking guidance on the recommended workflow for using conda environments with Claude Code, as the current behavior seems inconsistent and undocumented.

Current Situation

Scenario 1: VS Code Terminal → Claude Code

  1. Activate conda environment in VS Code terminal: conda activate thuts28x
  2. Launch Claude Code from activated terminal
  3. Claude Code inherits environment variables (CONDA_DEFAULT_ENV=thuts28x) but loses conda shell functions

Scenario 2: Docker Container → Claude Code

  1. Enter Docker container via Docker Desktop exec
  2. Launch Claude Code directly
  3. Same issue: conda environment variables present but shell functions missing

Problem Description

Claude Code inherits conda environment variables but cannot use conda activate:

echo $CONDA_DEFAULT_ENV
# Output: thuts28x ✅ (inherited correctly)

which conda  
# Output: /opt/miniconda3/condabin/conda ✅ (conda available)

conda activate thuts28x
# Output: CondaError: Run 'conda init' before 'conda activate' ❌

Current Workaround

The only working approach requires shell hook injection in each command:

eval "$(conda shell.bash hook)" && conda activate thuts28x && python script.py

However, this doesn't persist between commands (related to issue #2508 about environment variables).

Root Cause Analysis

Based on conda documentation and community analysis:

  1. Environment variables are inherited from parent shell (✅ working)
  2. Conda shell functions are missing because Claude Code's bash processes don't load ~/.bashrc
  3. conda activate requires both environment variables AND shell functions

This is similar to issues in:

  • Docker containers (RUN conda activate failures)
  • Non-interactive shells in CI/CD
  • Jupyter notebook \! commands

Questions for Anthropic Team

  1. What's the recommended conda workflow for Claude Code users?
  2. Should users rely on the workaround (eval hook && activate) for each command?
  3. Are there plans to support conda environments more natively?
  4. Would persistent shell sessions (addressing environment variable persistence) help with conda workflows?

Potential Solutions to Consider

  1. Documentation update: Add conda workflow best practices
  2. Shell configuration: Load conda hooks automatically in Claude Code bash sessions
  3. Alternative approaches: Recommend conda run -n env_name command instead of activate
  4. Environment detection: Auto-detect and configure inherited conda environments

Impact on Development

This affects many Python developers using:

  • Data science workflows with conda environments
  • Package management via conda/mamba
  • Environment-specific tooling and dependencies
  • Cross-platform development (Windows + Docker + SSH scenarios)

Additional Context

This issue is distinct from #2508 (environment variable persistence) but related - solving environment variable persistence might also help conda workflows, but the missing shell functions would still need to be addressed.

Thank you for guidance on the recommended conda workflow\!"

View original on GitHub ↗

5 Comments

isCopyman · 1 year ago

Update: conda run Testing Results

I've tested conda run as a potential workaround and found mixed results:

✅ What Works with conda run

# Correct Python environment selection
conda run -n thuts28x python -c "import sys; print(sys.executable)"
# Output: /opt/miniconda3/envs/thuts28x/bin/python ✅

# Basic conda environment variables
conda run -n thuts28x python -c "import os; print(os.environ.get('CONDA_DEFAULT_ENV'))"
# Output: thuts28x ✅

❌ What Doesn't Work with conda run

# Environment variables set via conda env config vars
conda env config vars set PYTHONPATH=. -n thuts28x
conda env config vars list -n thuts28x
# Output: PYTHONPATH = . (shows as set)

conda run -n thuts28x python -c "import os; print('PYTHONPATH:', os.environ.get('PYTHONPATH', 'NOT_FOUND'))"
# Output: PYTHONPATH: NOT_FOUND ❌

Required Workaround

Even with conda run, environment variables must be set manually:

PYTHONPATH=. conda run -n thuts28x python analysis/script.py

Conclusion

  • conda run solves the Python interpreter selection problem (no need for conda activate)
  • conda run does NOT solve the environment variable problem
  • This is related to the broader environment variable persistence issue (#2508)

So while conda run is better than the eval hook && activate workaround, it still requires manual environment variable management."

ilkerhk · 1 year ago

Here is a workaround that worked for me.

1) I removed conda initialization part from my .bashrc. So now in shell the $PATH does not contain any anaconda3 folder
2) go to your project folder and activate a conda env
3) start claude and enter "! which python" to verify that it is the one from the activated conda env

corneliusroemer · 1 year ago

I've made a StackOverflow question for the conda activation failure and the workarounds you have found already to increase visibility/findability: https://stackoverflow.com/questions/79742342/cannot-activate-conda-mamba-environment-in-claude-code-session

catherinewu contributor · 9 months ago

Thanks for flagging! I just pushed an update to the docs to clarify this behavior. The docs should be live later this week.

Pasting new docs here for reference:

Bash tool behavior

The Bash tool executes shell commands with the following persistence behavior:

  • Working directory persists: When Claude changes the working directory (e.g., cd /path/to/dir), subsequent Bash commands will execute in that directory. You can use CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR=1 to reset to the project directory after each command.
  • Environment variables do NOT persist: Environment variables set in one Bash command (e.g., export MY_VAR=value) are not available in subsequent Bash commands. Each Bash command runs in a fresh shell environment.

To make environment variables available in Bash commands, you have three options:

Option 1: Activate environment before starting Claude Code (simplest approach)

Activate your virtual environment in your terminal before launching Claude Code:

conda activate myenv
# or: source /path/to/venv/bin/activate
claude

This works for shell environments but environment variables set within Claude's Bash commands will not persist between commands.

Option 2: Set CLAUDE_ENV_FILE before starting Claude Code (persistent environment setup)

Export the path to a shell script containing your environment setup:

export CLAUDE_ENV_FILE=/path/to/env-setup.sh
claude

Where /path/to/env-setup.sh contains:

conda activate myenv
# or: source /path/to/venv/bin/activate
# or: export MY_VAR=value

Claude Code will source this file before each Bash command, making the environment persistent across all commands.

Option 3: Use a SessionStart hook (project-specific configuration)

Configure in .claude/settings.json:

{
  "hooks": {
    "SessionStart": [{
      "matcher": "startup",
      "hooks": [{
        "type": "command",
        "command": "echo 'conda activate myenv' >> \"$CLAUDE_ENV_FILE\""
      }]
    }]
  }
}

The hook writes to $CLAUDE_ENV_FILE, which is then sourced before each Bash command. This is ideal for team-shared project configurations.

See [SessionStart hooks](/en/hooks#persisting-environment-variables) for more details on Option 3.

github-actions[bot] · 8 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.