conda
python
virtual environments
python version
package management

How to create conda environment with specific python version?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Creating isolated environments is one of the most important habits in Python development. Without isolation, dependency upgrades in one project can silently break another. Conda environments solve this by bundling interpreter version and package set into a reproducible unit. Specifying Python version at creation time is critical when frameworks, compiled extensions, or deployment targets require strict compatibility.

A reliable workflow includes three parts: create with explicit constraints, verify the interpreter actually selected, and export lock-style metadata so teammates or CI can rebuild the same environment. This article covers practical commands and patterns that avoid common environment drift problems.

Core Sections

Create an environment with explicit Python version

Use conda create and pin major/minor version.

bash
conda create -n ml-py311 python=3.11

If you need a patch-level constraint, add it explicitly.

bash
conda create -n legacy-tooling python=3.9.18

Pinning early prevents accidental interpreter mismatches that can break compiled wheels or runtime behavior.

Activate and verify interpreter

Always verify after activation instead of assuming the solve result.

bash
1conda activate ml-py311
2python --version
3which python
4conda info --envs

Verification catches issues like shell initialization problems, wrong environment activation, or stale terminal sessions.

Install packages from stable channels

Channel strategy affects reproducibility. Prefer explicit channels and avoid ad-hoc mixing.

bash
conda install -n ml-py311 -c conda-forge numpy pandas scikit-learn

You can enforce strict priority:

bash
conda config --set channel_priority strict

This reduces dependency conflicts caused by resolving across incompatible channel builds.

Reproduce environment with YAML

Export environment specs so teammates and CI recreate the same setup.

bash
conda env export -n ml-py311 > environment.yml

Recreate elsewhere:

bash
conda env create -f environment.yml

For cleaner cross-platform files, consider exporting without build strings and reviewing pinned versions.

Update Python version intentionally

Changing Python version in an existing env can be risky if package constraints are tight.

bash
conda activate ml-py311
conda install python=3.12

For major upgrades, creating a fresh environment is usually safer than mutating a long-lived one.

Use environment.yml as source of truth

A minimal environment file keeps intent clear.

yaml
1name: analytics-py310
2channels:
3  - conda-forge
4dependencies:
5  - python=3.10
6  - pandas
7  - pyarrow
8  - pip
9  - pip:
10      - great-expectations==0.18.12

Store this in version control and update intentionally during dependency review.

Clean up unused environments

Old environments consume disk and create confusion.

bash
conda env list
conda remove -n old-experiment --all

Regular cleanup keeps your local setup manageable and reduces accidental use of obsolete envs.

Common Pitfalls

  • Creating environments without explicit Python pinning, then discovering incompatible versions during deployment.
  • Installing from mixed channels without priority rules, causing hard-to-debug dependency conflicts.
  • Assuming activation succeeded without checking interpreter path and version.
  • Mutating a heavily used environment repeatedly instead of rebuilding from a tracked specification file.
  • Forgetting to commit environment.yml, making team and CI environments drift over time.

Summary

Creating a conda environment with a specific Python version is simple, but reproducibility depends on disciplined workflow. Pin interpreter version at creation, verify activation, control channels, and export environment specs into version control. Prefer rebuilding over endlessly mutating old environments, especially for major upgrades. With these practices, your Python runtime remains predictable across development machines, CI pipelines, and production packaging.

For teams managing many environments, automate validation in CI by creating the environment from environment.yml on a clean runner. This quickly reveals unsatisfied constraints or removed package builds and keeps local developer setups aligned with build infrastructure. When releases matter, tag environment-file changes alongside application versions so rollbacks can restore both code and interpreter dependencies together.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.