Notebooks and your toolkit · lesson 4 of 6
Parts in this lesson
Apython -m venv .venva Python for this project only
Bpip installadd packages to it
Crequirements.txtthe list, so others can do the same
Djupyter labopen the notebook app

The toolkit on your computer

Last updated

This page runs Python for you, but real data lives on your computer, and so will your projects. In this lesson you'll set up a project with its own virtual environment, install the data tools into it, open JupyterLab, and learn why a notebook sometimes can't find a package you just installed.

By the end of this lesson you can

  • Make a project folder with its own virtual environment
  • Install the data tools with pip and record them in requirements.txt
  • Open a notebook in JupyterLab or VS Code with the project's Python as its kernel
  • Fix a ModuleNotFoundError caused by the wrong Python

Which Python?

You may already have Python from Simple Python lesson 2.9. If not, follow that lesson's install steps first, then come back. This lesson assumes Python 3.12 or newer is installed.

One computer can hold several Pythons, and the reason most setups go wrong is mixing them up. Step through this first.

Three Pythons on one computer: the system's Python with no packages, tindahan .venv with the data tools, and thesis .venv with an older pandas.Your computerThe system’s Pythontindahan/.venvthesis/.venv
1 of 5 One computer can have several Pythons: the one that came with it, and one virtual environment per project, each in its own .venv folder.

A project and its environment

Every project gets its own folder and its own virtual environment: a private copy of Python, in a folder called .venv, with its own packages. Then installing a package for one project can never break another.

Open a terminal (on Windows, PowerShell; on a Mac, Terminal) and type the lines that start with $, one at a time. Don't type the $.

Windows (PowerShell)
cd Documents
mkdir tindahan
cd tindahan
py -m venv .venv
.venv\Scripts\activate
(.venv) $ python --version
Python 3.14.0
macOS or Linux
cd ~/Documents
mkdir tindahan
cd tindahan
python3 -m venv .venv
source .venv/bin/activate
(.venv) $ python --version
Python 3.14.0

The last command, activate, switches this terminal to the project's Python. You can tell it worked because the prompt starts with (.venv). Each new terminal starts without it, so run activate again whenever you come back to the project.

If Windows says scripts are disabled when you activate, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, answer Y, and try again. It lets scripts you made yourself run, for your account only.

Install the data tools

With the environment active, install the tools this course uses, then save the exact list:

Terminal, in the tindahan folder
(.venv) $ python -m pip install numpy pandas matplotlib seaborn jupyterlab
Collecting numpy
...
Successfully installed ... jupyterlab-4... matplotlib-3... numpy-2... pandas-3... seaborn-0...
(.venv) $ python -m pip freeze > requirements.txt

python -m pip (instead of just pip) makes sure the packages go into the Python that's running, which is the active environment. It's a small habit that prevents the mistake in step 6.

pip freeze lists every installed package with its exact version, and > saves that list as requirements.txt. Anyone with the project folder (including you, on a new computer) can make the same environment in one line:

On another computer
py -m venv .venv
.venv\Scripts\activate
(.venv) $ python -m pip install -r requirements.txt

Keep requirements.txt with the project. Don't copy .venv to another computer or share it: it's big, and it only works on the computer that made it. Make a fresh one from requirements.txt instead.

Open JupyterLab

JupyterLab is the notebook app. Start it from the project folder, with the environment active:

Terminal
(.venv) $ jupyter lab
[I ServerApp] Jupyter Server is running at:
[I ServerApp] http://localhost:8888/lab?token=...
[I ServerApp] Use Control-C to stop this server.

It opens in your web browser, but it runs on your own computer: localhost means "this computer", and nothing is sent anywhere. The terminal has to stay open while you work; closing it (or pressing Ctrl+C in it) stops JupyterLab.

What you'll see:

  • The file browser on the left: your project's folders. Double-click a notebook to open it.
  • The launcher: click Python 3 (ipykernel) under Notebook to make a new one. Rename it by right-clicking its tab.
  • The toolbar over a notebook: run a cell (▶), stop, restart the kernel, and choose whether a cell is Code or Markdown.
  • The kernel name at the top right. It should be the project's Python.

Everything you practised in lessons 1.2 and 1.3 works the same there: Shift+Enter, In [n], Out[n], Markdown cells, Kernel → Restart Kernel and Run All Cells.

Notebooks in VS Code

If you use Visual Studio Code, you can skip JupyterLab: VS Code opens .ipynb files itself, once its Python and Jupyter extensions are installed.

  1. Open the tindahan folder (File → Open Folder).
  2. Make a file called 01-first-look.ipynb. VS Code shows it as a notebook.
  3. Click Select Kernel at the top right, then Python Environments, and choose the one in .venv.

The notebook works the same way. Pick whichever app you like: the notebook files are the same, and either can open the other's.

This page's notebooks also run Python, so you can check which one with code. Run these: on your own computer the version will be the one in your .venv.

which-python.ipynb

Wrong way: installed, but not found

The most common setup problem: you install pandas, open a notebook, type import pandas, and get ModuleNotFoundError: No module named 'pandas'. You did install it. But you installed it into one Python, and the notebook's kernel is another (step 1 showed exactly this).

Run the cells below to see the error for yourself. This page's notebook doesn't have seaborn yet, so import seaborn fails the same way.

imports.ipynb
  1. This page’s notebook has Python’s standard library, NumPy and pandas, but not seaborn yet: it arrives with the charts in Stage 7. So here, import seaborn fails exactly the way import pandas does on a computer where pandas isn’t installed in the notebook’s Python.

To fix it on your computer, check two things:

  1. Which kernel is the notebook using? In JupyterLab it's the name at the top right; in VS Code, the kernel button. Choose the project's .venv.
  2. Where did pip install? Activate the environment first, and use python -m pip install, so packages go into the Python you're running.

If JupyterLab doesn't list your .venv as a kernel, start JupyterLab from the activated environment (step 4), or run python -m pip install ipykernel in it.

Check what you learned

Why does each project get its own virtual environment?
A classmate wants to run your project. What do you send with your notebooks?
You ran pip install pandas, but the notebook still says No module named 'pandas'. What's the most likely cause?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis