Skip to content

Virtual Environments for Data Science

A virtual environment is an isolated Python setup for a specific project.

It keeps:

  • Python version (sometimes)
  • Installed libraries
  • Tooling (Jupyter, linters, etc.)

separate from other projects.

Data analytics projects often depend on:

  • Specific versions of NumPy/Pandas/Matplotlib
  • Jupyter
  • Database drivers
  • Visualization libraries

If you install everything globally, you will eventually face:

  • Version conflicts
  • “It worked yesterday” problems
  • Broken notebooks after updates

Virtual environments prevent most of those issues.

  • Comes with Python
  • Lightweight
  • Uses pip for packages
  • Great for data science libraries
  • Handles compiled packages easily
  • Works with both conda install and pip install
Section titled “Using venv (recommended for pure pip projects)”

From your project folder:

command
python -m venv .venv
  • macOS/Linux:
command
source .venv/bin/activate
  • Windows (PowerShell):
command
.\.venv\Scripts\Activate.ps1
command
pip install numpy pandas matplotlib seaborn jupyter

This creates a reproducible spec:

command
pip freeze > requirements.txt

Later someone can recreate the same installs:

command
pip install -r requirements.txt
Section titled “Using conda environments (recommended for analytics stacks)”
command
conda create -n analytics python=3.12
command
conda activate analytics
command
conda install numpy pandas matplotlib seaborn jupyter

Sometimes a package is not available in conda.

Recommended approach:

  1. Install as much as possible with conda
  2. Then install remaining packages with pip

Example:

command
conda install numpy pandas
pip install yfinance

Each project gets its own isolated Python + package set, so changes in one never break another.

diagram Environment isolation mermaid
Two separate projects each keep their own Python version and installed packages

Good names:

  • analytics
  • titanic-eda
  • viz

Avoid generic names like test or newenv.

Best practices for data analytics projects

Section titled “Best practices for data analytics projects”
  • Create one environment per project
  • Pin versions for important packages (especially for long projects)
  • Keep a requirements.txt (pip) or environment.yml (conda)
  • Store notebooks inside a project folder

Example conda environment file (environment.yml)

Section titled “Example conda environment file (environment.yml)”

This is a common way to share environment configuration:

environment.yml
name: analytics
channels:
  - conda-forge
dependencies:
  - python=3.12
  - numpy
  - pandas
  - matplotlib
  - seaborn
  - jupyter
  - pip
  - pip:
      - yfinance

Then create it with:

command
conda env create -f environment.yml

Continue to: Installing Data Science Libraries (pip & conda) to learn the best ways to install and verify common analytics libraries.

Exercise 1 – Model Two Isolated Environments

Section titled “Exercise 1 – Model Two Isolated Environments”

Exercise 2 – Validate an Environment Name

Section titled “Exercise 2 – Validate an Environment Name”

Exercise 3 – Build an environment.yml Dependency List

Section titled “Exercise 3 – Build an environment.yml Dependency List”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading