By the end of this lesson, you will be able to create an isolated Python environment, install data analytics libraries, and launch a Jupyter Notebook for analysis.
What it is
A virtual environment is a self-contained directory that holds a specific version of Python and its installed packages. This isolation prevents conflicts between projects; for example, one project might need pandas 1.5 while another requires pandas 2.0. Jupyter Notebook is an interactive web-based interface that allows you to combine code execution, visualizations, and narrative text in a single document. Together, they form the standard workflow for exploratory data analysis (EDA).
Why it matters
- Dependency Isolation: Prevents "dependency hell" where updating a library breaks unrelated projects.
- Reproducibility: Ensures your analysis runs identically on other machines by locking package versions.
- Interactive Exploration: Jupyter allows immediate feedback on data transformations and plots without re-running entire scripts.
- Clean Workspace: Keeps global system Python installations uncluttered with experimental packages.
Syntax or steps
- Open your terminal or command prompt.
- Create a new virtual environment using the built-in
venvmodule. - Activate the environment (syntax varies by OS).
- Install required packages using
pip. - Launch Jupyter Notebook.
Example
# Step 1: Create the environment named 'analytics_env'
python -m venv analytics_env
# Step 2: Activate the environment
# On macOS/Linux:
source analytics_env/bin/activate
# On Windows (Command Prompt):
analytics_env\Scripts\activate.bat
# Step 3: Install core data science libraries
pip install pandas numpy matplotlib jupyterlab
# Step 4: Launch JupyterLab
jupyter lab
Explanation: The first command creates a folder containing a private Python interpreter. Activation modifies your shell's path so that typing python or pip uses this local version instead of the system-wide one. Installing jupyterlab ensures the notebook server is available within this specific environment. Finally, launching jupyter lab opens a browser tab connected to this isolated kernel.
Common mistakes
- Forgetting to activate: If you run
pip installwithout activating the env, packages go to the global system, causing permission errors or version conflicts. - Using the wrong Python version: Ensure you use
python3if your system defaults to Python 2 (rare now, but possible). Check withpython --versionafter activation. - Hardcoding paths: Never write absolute paths to the virtual environment in your code. Always rely on the activated state.
- Not saving requirements: Forgetting to run
pip freeze > requirements.txtmakes it hard to share or reproduce the exact setup later.
When to use it
| Scenario | Recommended Tool | Reason |
|---|---|---|
| Quick script execution | Standard Python + venv | Lightweight, no UI overhead. |
| Exploratory Data Analysis | Jupyter Lab + venv | Visual feedback and cell-by-cell execution. |
| Production Deployment | Docker + venv | Containerization ensures consistency across servers. |
Practice
Guided Exercise: Create a new environment called test_env, activate it, and verify the installation location by running which python (Linux/Mac) or where python (Windows). It should point inside the test_env folder.
Challenge: Install seaborn in your active environment. Then, deactivate the environment (deactivate) and try to import seaborn in a standard Python shell. What happens? Why?
Hint: The import should fail because seaborn was only installed in the isolated environment, not globally.
Quick check
Q: How do you save the current list of installed packages in your virtual environment to a file?
A: Run pip freeze > requirements.txt while the environment is active.
Summary
Virtual environments provide essential isolation for Python projects, preventing dependency conflicts and ensuring reproducibility. Combining them with Jupyter Lab creates a powerful, controlled workspace for data analytics tasks. Always activate your environment before installing packages or running code.