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
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
Option 1: venv (built-in)
- Comes with Python
- Lightweight
- Uses
pipfor packages
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)
Create a new environment
From your project folder:
python -m venv .venvActivate the environment
- macOS/Linux:
source .venv/bin/activate- Windows (PowerShell):
.\.venv\Scripts\Activate.ps1Install packages
pip install numpy pandas matplotlib seaborn jupyterFreeze 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)
Create and activate
conda create -n analytics python=3.12conda activate analyticsInstall packages
conda install numpy pandas matplotlib seaborn jupyterMixing 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
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
Good names:
analyticstitanic-edaviz
Avoid generic names like test or newenv.
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)
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.ymlCheck your understanding
Which command creates a venv named .venv in your project folder?
python -m venv .venv uses the built-in venv module. conda create is for conda environments, and pip install venv is not how the built-in module is used.
Show answer
A — python -m venv .venv — python -m venv .venv uses the built-in venv module. conda create is for conda environments, and pip install venv is not how the built-in module is used.
You want others to recreate your pip installs exactly. Which pair of commands does the lesson show?
pip freeze writes the installed versions to requirements.txt, and pip install -r reads that file back. environment.yml belongs to conda, not pip freeze.
Show answer
A — pip freeze > requirements.txt, then pip install -r requirements.txt — pip freeze writes the installed versions to requirements.txt, and pip install -r reads that file back. environment.yml belongs to conda, not pip freeze.
When a package is not available in conda, what order does the lesson recommend?
Conda first, pip second. Mixing the two in random order can break the environment.
Show answer
B — Install as much as possible with conda first, then use pip for the rest — Conda first, pip second. Mixing the two in random order can break the environment.
Which command builds a conda environment from a shared environment.yml file?
conda env create -f reads the file and builds the environment. conda activate only switches into an existing one.
Show answer
C — conda env create -f environment.yml — conda env create -f reads the file and builds the environment. conda activate only switches into an existing one.
Next
Continue to: Installing Data Science Libraries (pip & conda) to learn the best ways to install and verify common analytics libraries.
🧪 Try It Yourself
Exercise 1 – Model Two Isolated Environments
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Validate an Environment Name
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Build an environment.yml Dependency List
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.