Python Central HubSign in

Data Analytics with Python / Phase 1: Environment and Setup

Virtual Environments for Data Science

Understand why virtual environments matter in data analytics and learn how to create them using venv and conda.

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.

Option 1: venv (built-in)

  • Comes with Python
  • Lightweight
  • Uses pip for packages

Option 2: conda environments

  • Great for data science libraries
  • Handles compiled packages easily
  • Works with both conda install and pip install

Create a new environment

From your project folder:

Shell
python -m venv .venv

Activate the environment

  • macOS/Linux:
Shell
source .venv/bin/activate
  • Windows (PowerShell):
Shell
.\.venv\Scripts\Activate.ps1

Install packages

Shell
pip install numpy pandas matplotlib seaborn jupyter

Freeze requirements

This creates a reproducible spec:

Shell
pip freeze > requirements.txt

Later someone can recreate the same installs:

Shell
pip install -r requirements.txt

Create and activate

Shell
conda create -n analytics python=3.12
Shell
conda activate analytics

Install packages

Shell
conda install numpy pandas matplotlib seaborn jupyter

Mixing conda + pip safely

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:

Shell
conda install numpy pandas
pip install yfinance

Visualize it

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

Environment naming conventions

Good names:

  • analytics
  • titanic-eda
  • viz

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) or environment.yml (conda)
  • Store notebooks inside a project folder

Example conda environment file (environment.yml)

This is a common way to share environment configuration:

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

Then create it with:

Shell
conda env create -f environment.yml

Check your understanding

quizCheck yourself
  1. Which command creates a venv named .venv in your project folder?

    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.

  2. You want others to recreate your pip installs exactly. Which pair of commands does the lesson show?

    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.

  3. When a package is not available in conda, what order does the lesson recommend?

    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.

  4. Which command builds a conda environment from a shared environment.yml file?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Validate an Environment Name

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Build an environment.yml Dependency List

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did