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

bash
conda --version

View basic help:

bash
conda --help

2) Create and Manage Environments

Create environment with specific Python version:

bash
conda create -n py-demo python=3.11

Activate environment:

bash
conda activate py-demo

Deactivate current environment:

bash
conda deactivate

List environments:

bash
conda env list

Remove environment:

bash
conda remove -n py-demo --all

3) Install and Remove Packages

Install package:

bash
conda install numpy

Install multiple packages:

bash
conda install pandas matplotlib

Remove package:

bash
conda remove pandas

Search package:

bash
conda search scikit-learn

Tip

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:

  1. create/activate conda environment
  2. install what you can with conda
  3. use pip only for missing packages

Example:

bash
conda activate py-demo
conda install numpy
pip install some-package-not-in-conda

This minimizes resolver conflicts.

5) Export and Rebuild Environments

Export environment:

bash
conda env export > environment.yml

Recreate environment from file:

bash
conda env create -f environment.yml

This 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

bash
conda create -n data-lab python=3.11
conda activate data-lab

Step 2: install dependencies

bash
conda install pandas matplotlib

Step 3: run script

python
import pandas as pd
 
df = pd.DataFrame({"name": ["Emma", "Liam"], "score": [95, 88]})
print(df)

Step 4: export setup

bash
conda env export > environment.yml

Common 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":

  1. Create env study-lab with Python 3.11
  2. Install numpy and requests
  3. Write a tiny script importing both
  4. Export environment.yml
  5. 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.