Sub topic 1
Why isolate your Python environment
Prerequisites
This chapter assumes Python 3 is installed, you can use a terminal or command prompt, and you know basic Python and CSV concepts.
Why isolate your Python environment
A Python environment is a directory containing a Python interpreter and project-specific packages. Isolation means that one project does not share package versions with another project.
Without isolation, installing a newer package for one project can break an older project. An isolated environment also makes the setup easier to reproduce on another computer.
The project directory can contain its own .venv directory:
sales-analysis/
├── .venv/
├── data/
└── analysis.ipynbThe .venv directory is usually excluded from version control. You record the packages instead of committing the environment itself.
Create a virtual environment for the project
Create or enter the project directory, then run:
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activateThe venv module creates the environment. Activation changes which python and pip commands your shell uses. Your prompt often shows (.venv) after activation.
Check that the command points into the project environment:
python -c "import sys; print(sys.executable)"Deactivate it later with deactivate.
Install JupyterLab, pandas, and matplotlib
Upgrade the package installer, then install the tools:
python -m pip install --upgrade pip
python -m pip install jupyterlab pandas matplotlibJupyterLab provides the browser-based development interface. pandas provides tables called DataFrames. matplotlib creates charts. Using python -m pip helps ensure that packages install into the active environment.
Start JupyterLab and find your way around
Start the server from the project directory:
jupyter labJupyterLab normally opens in a browser. The terminal displays a local URL containing an access token. Keep that terminal running while you work.
The left sidebar contains the file browser. Open or create a notebook there. A notebook contains cells that hold code, text, or results. The main work area displays the selected notebook, and the kernel runs its Python code.
Exercise
Why should you install packages after activating .venv?
Show answer
Activation makes the installation target this project’s isolated environment instead of a shared system Python installation.