Conda in Python: Manage Environments and Packages Across Languages
Introduction
In this chapter, you will learn conda, a popular environment and package manager widely used in Python data science and scientific computing. Conda can manage Python versions, dependencies, and isolated environments in a unified workflow. Once you understand conda, you can avoid many dependency conflicts and build reproducible development setups.
Prerequisites
- Python learning basics completed
- Basic command-line usage
- Basic understanding of package managers (
pip/ virtual environment)
What Is Conda
Conda is an environment and package manager that can manage:
- Python packages
- Python versions
- non-Python dependencies (in many cases)
- isolated environments
Conda is commonly distributed with:
- Anaconda (full distribution)
- Miniconda (minimal distribution)
1) Check Conda Installation
conda --versionView basic help:
conda --help2) Create and Manage Environments
Create environment with specific Python version:
conda create -n py-demo python=3.11Activate environment:
conda activate py-demoDeactivate current environment:
conda deactivateList environments:
conda env listRemove environment:
conda remove -n py-demo --all3) Install and Remove Packages
Install package:
conda install numpyInstall multiple packages:
conda install pandas matplotlibRemove package:
conda remove pandasSearch package:
conda search scikit-learnTip
Best Practice
Create one environment per project to reduce dependency conflicts and improve reproducibility.
4) Conda and pip Together
You can use both, but apply order carefully:
Suggested workflow:
- create/activate conda environment
- install what you can with
conda - use
piponly for missing packages
Example:
conda activate py-demo
conda install numpy
pip install some-package-not-in-condaThis minimizes resolver conflicts.
5) Export and Rebuild Environments
Export environment:
conda env export > environment.ymlRecreate environment from file:
conda env create -f environment.ymlThis is essential for teamwork and CI reproducibility.
6) Real Mini Example: Data Analysis Environment
Goal: build an isolated environment for a simple analysis project.
Step 1: create env
conda create -n data-lab python=3.11
conda activate data-labStep 2: install dependencies
conda install pandas matplotlibStep 3: run script
import pandas as pd
df = pd.DataFrame({"name": ["Emma", "Liam"], "score": [95, 88]})
print(df)Step 4: export setup
conda env export > environment.ymlCommon Beginner Mistakes
Mistake 1: Installing Packages into Base Environment
This pollutes global setup and causes future conflicts.
Mistake 2: Forgetting to Activate Target Environment
Commands may install packages into the wrong environment.
Mistake 3: Mixing pip/conda Without Strategy
Random mixing can produce hard-to-debug dependency issues.
Warning
Avoid daily development directly in base environment.
Use dedicated project environments instead.
Surprise Practice Challenge
Build a "Conda Reproducibility Drill":
- Create env
study-labwith Python 3.11 - Install
numpyandrequests - Write a tiny script importing both
- Export
environment.yml - Recreate the env on a fresh machine/folder and run script again
If you finish this, you are ready for dependable multi-project environment management.
FAQ
Should I use conda or pip?
Use conda when you need strong environment isolation and scientific/data ecosystem support; use pip for general Python package workflows or packages unavailable in conda channels.
Is Anaconda required to use conda?
No. Miniconda also provides conda with a smaller footprint.
Can conda manage Python version per project?
Yes. That is one of its key strengths.
Should environment.yml be committed?
Yes, for collaborative and reproducible projects.