How to change the Python version of the conda virtual environment?

Change The Python Version Of An Existing Conda Virtual Environment (1)

Conda is a powerful open-source package and environment manager for Python that allows users to create, manage, and switch between virtual environments. Changing a Python version of the conda virtual environment is crucial when transitioning between projects that require different Python versions or when updating an existing project to a new Python release.

The Conda virtual environment is an isolated environment where packages and dependencies can be installed without affecting the system’s global Python installation. They prevent conflicts between projects and make it easy to switch between Python versions. Conda allows users to create and manage these environments effortlessly.

With frequent updates and versions, it is necessary to know how to manage different Python versions for your projects. In this article, we will guide you through the process of changing the Python version in an existing conda virtual environment.

To change the Python version in a Conda virtual environment, activate the environment using conda activate env_name, check the current version with python --version, list available versions using conda search '^python$', and install a new version with conda install python=x.y. This process helps adapt to different project requirements.


Why Change to Different Python Versions

Before starting the process of changing the Python version of an existing conda environment, let’s discuss why. Different Python versions have different features and performance improvements. Also, some libraries or packages may only be compatible with certain Python versions. So switching between Python versions in the conda environment is crucial for your projects.

Also read: Python Anaconda Tutorial – Getting Started With Anaconda


Easy Steps to Update Python in Conda Environments

Here is a step-by-step guide on how to change the Python version of existing conda virtual environments.

1. Activate conda virtual environment

The first step is to activate the conda virtual environment. For the environment in which you want to change the Python version, activate that conda virtual environment. Open a terminal. Here we will use Anaconda Prompt. Use the following command to activate the conda environment:

conda activate env1

This command line activates your conda virtual environment. You can replace the env1 with the actual name of your environment for which you want to change the Python version.

Conda Activate

Here you can see we are in the base environment but after activating the env1 we are now in the env1 environment.

You can directly open a terminal of the environment for which you want to change the Python version by using Anaconda Navigator.

Conda Env1T

You can see the option Open Terminal, click on it. Now you are in the env1 virtual environment.

2. Check the current Python version

You can check the current Python version by using the following command:

python --version

This command gives the current Python version.

Conda Pyv

The current Python version of the env1 environment is 3.7.16.

3. List available Python versions

Conda provides a list of available Python versions that can be installed in your virtual environment. Use the following command to get the list of available Python versions.

conda search "^python$"

This command provides you with a list of available Python versions.

Conda Pyversionlist

From the above list identify the version you want to switch to.

Also read: Conda vs Pip: Choosing your Python package manager

4. Change Python version

Now to change the Python version within the active conda virtual environment, use the following command:

conda install python=3.8

Here we use Python version number 3.8. You can replace the Python version with the version number you want to install. Conda will install the specified Python version.

Conda Installpy 1

Enter y to proceed with the installation.

3. Check Python version

After the installation is complete, you can verify the Python version has been successfully changed by using the following command:

Conda Installpy1

Now you can see the Python version is 3.8.18. Ensure that the displayed Python version matches with the Python version you intended to install.


Conclusion

Now you know how to switch Python versions in your Conda environments! It’s a simple yet powerful skill that keeps your projects flexible and up-to-date. Whether you’re adapting to new project requirements or exploring the latest Python features, this knowledge puts you in control. Remember, the right Python version can make all the difference in your coding journey. What exciting project will you tackle next with your newly updated environment?


References

https://stackoverflow.com/questions/59163078/how-to-change-python-version-of-existing-conda-virtual-environment