How to create conda environment with specific python version?

Learn how to create conda environment with specific python version? with practical examples, diagrams, and best practices. Covers python, conda, miniconda development techniques with visual explana...

Creating Conda Environments with Specific Python Versions

Hero image for How to create conda environment with specific python version?

Learn how to effectively manage your Python projects by creating isolated Conda environments, each configured with a precise Python version to avoid dependency conflicts.

Conda is an open-source package management system and environment management system that runs on Windows, macOS, and Linux. It's particularly powerful for data science and machine learning workflows, allowing users to create isolated environments with specific versions of Python and other packages. This isolation prevents conflicts between project dependencies, ensuring reproducibility and stability across different projects.

Why Specify Python Versions?

Different Python projects often require different Python versions or specific versions of libraries that might not be compatible with each other. For example, a legacy project might run on Python 3.6, while a new project requires Python 3.9. Attempting to install all dependencies in a single global environment would quickly lead to 'dependency hell'. Conda environments solve this by allowing you to create separate, self-contained spaces for each project, each with its own Python interpreter and set of installed packages.

flowchart TD
    A[Start Project] --> B{Requires Python 3.6?}
    B -->|Yes| C[Create Conda Env (Python 3.6)]
    B -->|No| D{Requires Python 3.9?}
    D -->|Yes| E[Create Conda Env (Python 3.9)]
    D -->|No| F[Create Conda Env (Default Python)]
    C --> G[Install Project Dependencies]
    E --> G
    F --> G
    G --> H[Develop Project]
    H --> I[End Project]

Workflow for selecting Python versions in Conda environments.

Basic Conda Environment Management

Before creating an environment with a specific Python version, it's good practice to ensure your Conda installation is up to date. This helps prevent issues and ensures you have access to the latest features and package versions.

conda update conda
conda update --all

Update Conda and all installed packages in the base environment.

Creating an Environment with a Specific Python Version

The core command for creating a new Conda environment is conda create. To specify a Python version, you simply append python= followed by the desired version number. Conda will then find and install that specific Python interpreter within the new environment.

1. Create the environment

Use the conda create command, specifying the environment name and the desired Python version. For example, to create an environment named myenv with Python 3.8:

2. Activate the environment

Once created, you need to activate the environment to start using it. This changes your shell's PATH to point to the Python and packages within that environment:

3. Verify the Python version

After activation, you can confirm that the correct Python version is being used:

4. Deactivate the environment

When you're done working in an environment, you should deactivate it to return to your base or previous environment:

Create Environment

conda create --name myenv python=3.8

Activate Environment

conda activate myenv

Verify Python

python --version

Deactivate Environment

conda deactivate

Listing and Removing Environments

Managing your environments effectively involves knowing how to list existing ones and remove those you no longer need. This helps keep your system clean and organized.

conda env list
# or
conda info --envs

List all Conda environments on your system.

conda env remove --name myenv

Remove a Conda environment named 'myenv'.