TutorialsPythonVirtual Environments and Package Management

Virtual Environments and Package Management

Set up isolated Python environments and manage packages with pip and conda

Virtual environments solve the "it works on my machine" problem. They let each project have its own Python packages and versions — preventing conflicts between projects. This is professional best practice that every data analyst should know.

Example

venv and pip workflow
# CREATE a virtual environment
python -m venv data_env           # creates a "data_env" folder

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

# You will see (data_env) in your prompt — you are now isolated

# INSTALL packages
pip install pandas numpy matplotlib seaborn jupyter openpyxl

# SEE installed packages
pip list
pip freeze > requirements.txt    # save to file

# INSTALL from requirements file (reproduce on another machine)
pip install -r requirements.txt

# DEACTIVATE
deactivate

# ESSENTIAL DATA ANALYST PACKAGES:
pip install pandas numpy matplotlib seaborn jupyter
pip install openpyxl xlsxwriter   # Excel support
pip install sqlalchemy pyodbc     # Database connections
pip install scikit-learn          # Machine learning (optional)
pip install duckdb                # Fast SQL on DataFrames
💡 requirements.txt is how you share your environment — always include it in your projects on GitHub.

Key Points

  • Always create a virtual environment for each project — never install globally
  • pip freeze > requirements.txt captures all installed packages and their exact versions
  • conda is an alternative to pip/venv — preferred when working with Anaconda distribution
  • If on Anaconda: conda create -n myenv python=3.11 and conda activate myenv
  • Include requirements.txt in every GitHub project so others can reproduce your environment

Practice Question

Which command saves all currently installed packages to a requirements.txt file?

Related Topics

Python IntroductionWhy Python is the top data analyst skill in India and how to get started in minutesPython Best Practices for Data AnalystsWrite clean, readable, professional Python code — habits that matter in team environments