TutorialsPythonVirtual Environments and Package Management

Virtual Environments and Package Management

Manage Python packages properly with venv and pip — avoid the "works on my machine" problem

A virtual environment is an isolated Python installation for a specific project. It keeps project dependencies separate — so "Project A" can use pandas 1.5 and "Project B" can use pandas 2.0 without conflict. This matters for data analyst roles because companies often have strict package versions and you will need to replicate environments on new machines or share code with colleagues.

Example

Setting up a Python environment for data work
# CREATE a virtual environment
python -m venv data_env

# ACTIVATE
# Windows:
data_env\Scripts\activate
# Mac/Linux:
source data_env/bin/activate

# INSTALL packages
pip install pandas numpy matplotlib seaborn openpyxl jupyter sqlalchemy

# SAVE requirements
pip freeze > requirements.txt
# Contents: pandas==2.2.1, numpy==1.26.4, ...

# RECREATE on another machine
pip install -r requirements.txt

# CHECK installed packages
pip list
pip show pandas   # version and location

# DEACTIVATE
deactivate

# ANACONDA ALTERNATIVE (popular for data science)
conda create -n data_env python=3.11
conda activate data_env
conda install pandas numpy matplotlib seaborn jupyter
💡 Always create a requirements.txt and add it to your GitHub repo — this lets anyone replicate your exact environment.

Key Points

  • Never install packages in the global Python installation for project work — always use venv
  • pip freeze > requirements.txt saves the exact versions of all installed packages
  • pip install -r requirements.txt installs everything from requirements.txt
  • Anaconda is popular in data science — it includes Python, Jupyter, and 250+ data packages pre-installed
  • In VS Code: select interpreter (Ctrl+Shift+P → Python: Select Interpreter) to use your venv

Practice Question

You want to share your Python data project with a colleague so they can run it with identical package versions. What file should you include?