Virtual Environments for Data Science
What is a virtual environment?
Section titled “What is a virtual environment?”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.
Why it’s essential in data analytics
Section titled “Why it’s essential in data analytics”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.
Two popular choices
Section titled “Two popular choices”Option 1: venv (built-in)
Section titled “Option 1: venv (built-in)”- Comes with Python
- Lightweight
- Uses
pipfor packages
Option 2: conda environments
Section titled “Option 2: conda environments”- Great for data science libraries
- Handles compiled packages easily
- Works with both
conda installandpip install
Using venv (recommended for pure pip projects)
Section titled “Using venv (recommended for pure pip projects)”Create a new environment
Section titled “Create a new environment”From your project folder:
python -m venv .venvActivate the environment
Section titled “Activate the environment”- macOS/Linux:
source .venv/bin/activate- Windows (PowerShell):
.\.venv\Scripts\Activate.ps1Install packages
Section titled “Install packages”pip install numpy pandas matplotlib seaborn jupyterFreeze requirements
Section titled “Freeze requirements”This creates a reproducible spec:
pip freeze > requirements.txtLater someone can recreate the same installs:
pip install -r requirements.txtUsing conda environments (recommended for analytics stacks)
Section titled “Using conda environments (recommended for analytics stacks)”Create and activate
Section titled “Create and activate”conda create -n analytics python=3.12conda activate analyticsInstall packages
Section titled “Install packages”conda install numpy pandas matplotlib seaborn jupyterMixing conda + pip safely
Section titled “Mixing conda + pip safely”Sometimes a package is not available in conda.
Recommended approach:
- Install as much as possible with
conda - Then install remaining packages with
pip
Example:
conda install numpy pandas
pip install yfinanceVisualize it
Section titled “Visualize it”Each project gets its own isolated Python + package set, so changes in one never break another.
flowchart LR
subgraph EnvA["Env: titanic-eda"]
A1["Python 3.10"] --> A2["pandas 1.5, numpy 1.24"]
end
subgraph EnvB["Env: forecasting"]
B1["Python 3.12"] --> B2["pandas 2.2, statsmodels 0.14"]
end
Global["Global / base Python"] -.->|"kept untouched"| EnvA
Global -.->|"kept untouched"| EnvB
Environment naming conventions
Section titled “Environment naming conventions”Good names:
analyticstitanic-edaviz
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) orenvironment.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:
name: analytics
channels:
- conda-forge
dependencies:
- python=3.12
- numpy
- pandas
- matplotlib
- seaborn
- jupyter
- pip
- pip:
- yfinanceThen create it with:
conda env create -f environment.ymlContinue to: Installing Data Science Libraries (pip & conda) to learn the best ways to install and verify common analytics libraries.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”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.coffeeCtapch.feedbackHeading
pch.feedbackSubheading