Virtual Environments & Git Version Control
Isolate project dependencies with venv/conda and track experiments safely with Git workflows.
In plain words
A virtual environment keeps each project's tools in its own box, and Git keeps a saved history of every change so you can go back.
Think of it like…
A lab notebook with separate labelled boxes of equipment: each experiment has its own box of tools (the environment) so nothing contaminates anything else, and the notebook pages are dated and never torn out (the Git history).
Where you’ve seen it
A forecasting model gives different numbers on a colleague's laptop. pip freeze shows they have numpy 2.1 and statsmodels 0.13, while the original environment used numpy 1.26 and statsmodels 0.14. The team adds a pinned requirements.txt (or a lock file), a README with the setup commands, and a Git tag for the release that produced the reported numbers, so anyone can recreate the exact run.
Words you’ll hear
- Virtual environment
- An isolated set of installed packages tied to one project.
- requirements.txt / lock file
- A list of exact package versions needed to recreate the environment.
- Commit / branch
- A saved snapshot of changes / a parallel line of work that can be merged.
- .gitignore
- A list of files Git should not track, such as data, secrets and environments.
Visual diagram
Theory & deep dive
Environments in practice
Create one environment per project: python -m venv .venv, activate it (source .venv/bin/activate or .venv\Scripts\activate on Windows), then pip install pandas scikit-learn. Save exact versions with pip freeze > requirements.txt, and recreate them with pip install -r requirements.txt. For tighter control, tools such as pip-tools, Poetry, uv or conda-lock produce lock files that also pin sub-dependencies. conda or mamba environments are popular when you need non-Python libraries (CUDA, GDAL) or several Python versions.
Use a project-specific Jupyter kernel (python -m ipykernel install --user --name myproject) so notebooks run in the right environment, record the Python version, and avoid pip install inside notebook cells without recording it. A short README with the setup commands is part of the project, not an extra.
python -m venv .venv, source .venv/bin/activate, pip install pandas scikit-learn jupyter, pip freeze > requirements.txt, then git init and add .venv/ to .gitignore.
A Git workflow for analysis work
The core loop is git status, git add <files>, git commit -m "Fix timezone bug in order dates", git push. Commit small, coherent changes with messages that say why. Use a branch per task (git switch -c feature/churn-features), open a pull request for review, then merge. git log --oneline shows history, git diff shows changes, and git restore <file> or git revert <commit> undoes mistakes safely.
Notebooks diff badly because they contain outputs; clear outputs before committing, or use tools like nbstripout, jupytext (pairing notebooks with plain .py) or review notebooks in a viewer that understands them. Keep large data and model files out of Git (use DVC, Git LFS or object storage), keep secrets in environment variables or a secrets manager rather than in code, and tag releases that produced published results (git tag report-2026-q4). If a secret is committed, rotate it: deleting the file doesn't remove it from history.
git switch -c try-xgboost, commit the experiment, and compare metrics. If it wins, merge; if not, delete the branch. The main branch always stays runnable.
How it works, step by step
Step 1 of 41. Create the environment
Make a project folder, create and activate a virtual environment, and install only what you need.
- Per project
- one env
When to use it & trade-offs
What it is
A virtual environment (python -m venv .venv, or a conda environment) is an isolated folder of Python packages for one project, so project A can use pandas 1.5 while project B uses pandas 2.2. Git records every change to your code as a commit, so you can compare versions, undo mistakes, work on branches and share through a remote such as GitHub. Together they answer "it worked yesterday" and "it works on my machine".
Why it matters: Data work breaks silently when package versions drift: a model trained with one scikit-learn version may load differently in another, and a notebook that ran last month stops running. Reproducible environments and version control are the minimum expectation for team projects, and interviews often ask how you'd make a result reproducible.
When to reach for it
- A separate environment for every project
- Pinned requirements for anything you report or deploy
- Branches and pull requests for team analysis work
When to avoid or be careful
- Installing everything into the global Python
- Committing data files, keys or
.venv - Committing notebooks with huge outputs
Environment tools
Strengths: Built in; lightweight.
Watch out: Doesn't manage non-Python libraries or Python versions.
Strengths: Manages Python and system libraries.
Watch out: Heavier; environments can be slow to solve.
Strengths: Exact, resolved lock files; fast installs (uv).
Watch out: Another tool to learn.
Interview signals & numbers to know
When this topic comes up
- “It works on my machine”
- “How do you make results reproducible?”
- “We leaked an API key in the repo”
- Dependency pinning
- Notebook version control
Benchmarks & rules of thumb
- 1
- environment per project
- 0
- secrets allowed in a repository
- small
- preferred commit size
Common mistakes & follow-ups
Common interview mistakes
- Using the global Python for everything and then breaking two projects with one upgrade.
- Committing a
.envfile with an API key, then simply deleting it in the next commit. - Storing a 2 GB CSV in Git, making every clone slow.
Likely interviewer follow-ups
- How would you make a data science project reproducible?
- What goes in
.gitignorefor a data project? - How do you review changes to notebooks?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Virtual Environments & Git Version Control with data science interview Q&A and hands-on practice.