How to change the default Anaconda Python environment

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# Table of Contents

  1. How to change the default Anaconda Python environment
  2. Permanently changing your default Anaconda environment on macOS and Linux
  3. Permanently changing your default Anaconda environment on Windows

# How to change the default Anaconda Python environment

If you need to temporarily change the environment, use the conda activate ENVIRONMENT_NAME command.

shell
conda activate <ENVIRONMENT_NAME>

conda temporarily change default environment

If you need to list your environments, use the conda env list command.

shell
conda env list

conda list environments

If you need to create a virtual environment, use the following command.

shell
conda create --name my-env

If you are in a conda environment that uses Python version 3.9.2 and want to update it to the latest version in the 3.9 branch, then use the following command.

shell
conda update python

You can also upgrade the Python in a conda environment to a different version, e.g. from 3.9 to 3.10, however, this is not recommended.

It is usually safer to create a new environment.

If you want to create a new environment using a specific Python version, use the following command.

shell
conda create -n <YOUR_ENV_NAME> python=3.10 anaconda

Replace the <YOUR_ENV_NAME> placeholder with the actual name of the environment.

The command installs version 3.10 of Python.

If you want to upgrade the Python version of an existing conda environment, use the following command.

shell
conda install python=3.10

If you want to permanently change your default Anaconda environment, click on the subheading that relates to your operating system:

# Permanently changing your default Anaconda environment on macOS and Linux

If you need to permanently change your default conda environment, you have to add a command to your startup script (e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc).

You can use the gedit command to edit your profile file.

shell
sudo gedit ~/.bashrc sudo gedit ~/.bash_profile sudo gedit ~/.zshrc

Or the nano command if you prefer to add the line in your terminal.

shell
sudo nano ~/.bashrc sudo nano ~/.bash_profile sudo nano ~/.zshrc

Once you open your profile file, add the activation command at the end of the file.

shell
conda activate <ENVIRONMENT_NAME>

In my case, the command looks as follows:

shell
conda activate my-env

Save the file and source your profile file from your terminal.

shell
source ~/.bashrc source ~/.bash_profile source ~/.zshrc

If you open a new terminal window, you will see that it defaults to the specified conda environment.

changed default anaconda environment using profile file

You can also use the conda env list to print the active environment.

shell
conda env list

print active environment macos linux

The active environment has an asterisk * next to its name.

# Permanently changing your default Anaconda environment on Windows

To permanently change your default Anaconda environment on Windows:

  1. Open the C:\Users\YOUR_USER\AppData\Roaming directory.
  2. Create a batch file command named my_conda_env.cmd in the AppData directory.

The file should contain your conda activation command.

shell
conda activate <ENVIRONMENT_NAME>

create batch file command

  1. Now, you have to configure the file to run automatically when CMD (Command Prompt) is started.

  2. Search for Registry and start the Registry Editor application.

start registry editor application

  1. The name of the key is HKEY_CURRENT_USER\Software\Microsoft\Command Processor.

You might have to create the Command Processor key under Microsoft if it doesn't already exist.

You simply have to right-click on Microsoft and select New > Key.

  1. In the Command Processor key, right-click in the pane on the right-hand side and select New > Expandable String Value.

create expandable string value

Set the Value name: to AutoRun.

Set the Value data: to %AppData%\my_conda_env.cmd.

set value name and value data

The next time you open CMD (Command Prompt), the C:\Users\YOUR_USER\AppData\Roaming\my_conda_env.cmd file will run.

The file will use the conda activate command to activate your preferred conda environment automatically.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.