Python
Beginner 12 min read Python

Python for Data Science Setup

From bare Python to a working workspace

A data science environment is Python plus numpy, pandas, matplotlib/scikit-learn and a project layout — managed with venvs or conda so notebooks and pipelines run identically anywhere.

pythonvenvcondapipjupyterrequirements.txt
Before we start

What you will learn

  • Create and activate an isolated virtual environment
  • Pin dependencies reproducibly
  • Start Jupyter and run cells
Introduction

What is this?

Python alone is not a data science environment: analysis needs numpy, pandas, scipy, matplotlib and scikit-learn, plus a way to isolate them per project. The standard setup is a virtual environment (venv/conda) with pinned requirements, a Jupyter server or IDE for exploration, and a reproducible file layout — the same 'works on any machine' guarantee containers give to apps.

Motivation

Why do you need it?

  • Different projects need different library versions; a shared global site-packages is a time bomb.
  • Reproducibility is the whole point: pin versions and anyone (or any pipeline) rebuilds the same stack.
  • Notebook exploration + `.py` scripts + pinned env is the canonical DS workflow.
  • A broken env is the most common first-hour blocker in any DS course.
Think about it

Mental model

Imagine

A per-kitchen pantry vs one communal pantry for a whole building

Think of

Virtual environments vs global packages

The building's pantry overwrites everyone's flour; each kitchen with its own shelf of pinned ingredients cooks dependably. venvs give every project its own shelf.

Mechanics

How it works

  • python -m venv .venv creates an isolated interpreter + package folder in the project.
  • Activating it switches the shell's python/pip to the project's copies.
  • pip install pins exact versions; a requirements.txt freezes them for reproducibility.
  • Jupyter (jupyter lab) runs notebooks in the same kernel as the venv when installed there.
  • conda wraps python itself and native libs, handy when numpy/scipy need compiled binaries.

Anatomy of a reproducible DS setup.

Project folder
venv
requirements.txt
Jupyter / IDE
Reproducible run
Reference

Key commands

python -m venv .venv

Create an isolated environment in .venv.

source .venv/bin/activate

Activate it (Windows: .venv\Scripts\activate).

pip install numpy pandas scikit-learn

Install the core stack into the venv.

pip freeze > requirements.txt

Pin exact installed versions for reproducibility.

jupyter lab

Launch the notebook server.

Real world

In production

  • A data team rebuilds every analysis from requirements.txt in CI so notebooks ran in October still run in March.
  • A model-serve platform bundles the exact python+numpy+pandas versions into the serving image from a frozen env.
  • An MLOps studio gives every experiment its own conda env recorded in the experiment tracker.
Guidelines

Best practices

  • Create a venv per project, not per machine.
  • Pin exact versions (==), not ranges, before shipping an analysis.
  • Keep notebooks thin: heavy logic lives in .py modules imported by the notebook.
  • Commit requirements.txt and the recreate command, not the environment itself.
Watch out

Common mistakes

  • pip install into the base environment, later breaking another project.
  • Committing the .venv folder into git.
  • Running jupyter from system python while libraries are in the venv → ModuleNotFoundError.
  • Using `pip install python` instead of creating a venv (pip isn't a python substitute).
When things break

Debugging tips

  • 'ModuleNotFoundError in Jupyter but pip shows installed' → the kernel isn't the venv; check which python jupyter runs.
  • which python / which pip tells you what interpreter you're addressing.
  • python -c 'import pandas; print(pandas.__version__)' from the shell validates the venv.
Interviews

Interview checkpoint

Practice

Hands-on lab

Build a Reproducible Env
20 min · beginner
In progress
Objectives
  • Create and activate a venv
  • Install the core DS stack
  • Freeze requirements and validate imports

Build a Reproducible Env

20 min · beginner

0/5 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Which command pins your environment's exact versions to a file?

2

You should commit your .venv folder to git so teammates can copy the environment.

Go deeper

Official documentation