How to Stop Conda from Automatically Activating When VSCode Starts

How to Stop Conda from Automatically Activating When VSCode Starts

Photo of author
Written By Eric Sandler

If you’re using Visual Studio Code (VSCode) with Anaconda for Python development, you might notice that conda automatically activates its environment every time you open a terminal in VSCode. While this behavior is often useful for ensuring that the correct environment is activated for Python projects, it can sometimes be unnecessary or cause conflicts if you don’t need to use conda for every project.

This post will guide you through the steps to stop conda from automatically activating when VSCode starts, while still allowing you to activate it manually when needed.

Why Does Conda Automatically Activate?

By default, Anaconda modifies your shell initialization files (such as .bashrc, .zshrc, or profile.ps1 on Windows) to automatically activate its base environment every time a new terminal is opened. This is convenient when working in data science environments, but if you’re working on non-conda projects or use multiple Python environments, this automatic activation can get in the way.

How to Stop Conda from Automatically Activating

Step 1: Disable Auto-Activation of Conda in VSCode

VSCode can be configured to prevent conda from automatically activating the base environment whenever you open a new terminal. This setting is located in VSCode’s configuration file.

  1. Open VSCode.
  2. Access Settings by going to the menu and selecting File > Preferences > Settings (or press Ctrl + , on Windows/Linux or Cmd + , on macOS).
  3. In the settings search bar, type "terminal.integrated.env". This will show settings related to the terminal environment in VSCode.
  4. Look for the setting called Python: Conda Path and Python: Conda Auto Activate Base. You can also modify this setting directly in the settings.json file:
  • Open the command palette (Ctrl + Shift + P on Windows/Linux, Cmd + Shift + P on macOS) and search for Preferences: Open Settings (JSON).
  • Add the following line to the settings.json file to disable the automatic activation of conda’s base environment:
    json "python.condaAutoActivateBase": false

This setting will prevent conda from automatically activating the base environment when VSCode starts, but you will still be able to activate specific conda environments manually when needed.

Step 2: Modify Shell Initialization Files (Optional)

If conda activation still persists, you can also modify your shell’s initialization files to stop conda from loading its base environment by default.

For Bash or Zsh (Linux/macOS)
  1. Open the terminal and edit the .bashrc or .zshrc file:
   nano ~/.bashrc

or for Zsh:

   nano ~/.zshrc
  1. Find the section that looks like this:
   # >>> conda initialize >>>
   # !! Contents within this block are managed by 'conda init' !!
   __conda_setup="$('/path-to-conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
  1. Comment out or remove the lines that initialize conda automatically:
   # __conda_setup="$('/path-to-conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
  1. Save and close the file, then reload the shell configuration:
   source ~/.bashrc

or for Zsh:

   source ~/.zshrc
For PowerShell (Windows)
  1. Open PowerShell as an administrator.
  2. Edit your PowerShell profile by typing the following:
   notepad $PROFILE
  1. Find and comment out or remove the lines that activate conda automatically, which usually look like this:
   & "C:/path-to-conda/Scripts/conda.exe" "shell.powershell" "hook" | Out-String | Invoke-Expression
  1. Save and close the file, then restart PowerShell.

Step 3: Activate Conda Manually When Needed

Once you have disabled automatic activation, you can manually activate your conda environments in the VSCode terminal or in any other terminal session by running:

conda activate myenv

Where myenv is the name of the conda environment you want to activate.

Conclusion

By disabling the automatic activation of conda in VSCode and adjusting your shell settings, you gain more control over when and how conda environments are activated. This is especially useful if you’re working on projects that don’t require conda or if you manage multiple Python environments. You can always activate your conda environment manually when needed, ensuring that you have flexibility without the hassle of constant automatic activations.

Eric Sandler

Latest Early Black Friday Deals

Leave a Comment