How can I conda change Python version in an existing virtual environment?

I created a conda environment with Python 3.8, but it doesn’t support matplotlib. I’m looking for a way to change the Python version in my environment, something like conda env my_env update to python=3.6. Is this possible, or do I need to recreate the environment?

I have Miniconda installed.

You can directly update the Python version in the environment using the following command:

conda activate my_env
conda install python=3.6

This will update the Python version to 3.6 in your existing environment without needing to recreate it. Conda will attempt to resolve dependencies, ensuring compatibility.

If you prefer starting fresh without disrupting your current environment, you can clone your environment and specify the new Python version:

conda create --name my_new_env python=3.6 --clone my_env

This method allows you to keep the old environment while creating a new one with the desired Python version. You can then activate and use the new environment.

If your environment uses a .yml configuration file, you can manually change the Python version in the environment.yml file and then update the environment:

name: my_env dependencies:

  • python=3.6
  • other-packages

After updating the file, run:

conda env update --file environment.yml

This will modify the environment as per the updated Python version specified in the YAML file.