Notebooks and your own computer
Last updated
This page runs Python for you, and it will until Stage 6. From Stage 7 the models get bigger, and PyTorch can't run in a browser, so you'll train them on your own computer. In this lesson you'll set that up now, while the tools are small: a project folder with its own Python, the course's packages installed into it, and a notebook app that uses it. You'll also fix the most common setup problem there is.
By the end of this lesson you can
- Make a project folder with its own virtual environment
- Install NumPy, pandas, matplotlib and scikit-learn with
pip, and record them inrequirements.txt - Open a notebook in JupyterLab or VS Code using the project's Python
- Fix a
ModuleNotFoundErrorcaused by installing into the wrong Python
What a notebook is
Every code box in this stage has been a notebook: cells of code you run one at a time, which share one memory, with each cell's result shown under it. It's the standard way to explore data and try out models, because you can load the data once and then run the next idea without starting over.
A notebook is a file ending in .ipynb. Behind it runs a kernel: a Python program that runs the cells and keeps their names and values. Two apps open notebooks: JupyterLab, which runs in your web browser, and VS Code. Both use the same files.
Notebooks have one trap you've already met in this page: cells can be run in any order, so a notebook can work only because you ran a cell earlier that you've since deleted. Before you trust a notebook's results, restart its kernel and run every cell from the top.
You may already have Python from Simple Python lesson 2.9. If not, follow that lesson's install steps first, then come back. Most setup problems come from having more than one Python on a computer and mixing them up. Step through this first:
.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 or upgrading a package for one project can never break another. That matters in machine learning, where projects often need different versions of the same library.
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 $.
cd Documents mkdir ml-course cd ml-course py -m venv .venv .venv\Scripts\activate (.venv) $ python --version Python 3.14.0
cd ~/Documents mkdir ml-course cd ml-course python3 -m venv .venv source .venv/bin/activate (.venv) $ python --version Python 3.14.0
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 tools
With the environment active, install what Stages 0 to 6 use, then save the exact list:
(.venv) $ python -m pip install numpy pandas matplotlib scikit-learn jupyterlab Collecting numpy ... Successfully installed ... jupyterlab-4... matplotlib-3... numpy-2... pandas-3... scikit-learn-1... (.venv) $ python -m pip freeze > requirements.txt
python -m pip(instead of justpip) 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 5.- The package is called
scikit-learnwhen you install it, but you import it assklearn. You'll use it from Stage 2. - PyTorch isn't in the list yet. It's large and its install depends on your computer; lesson 7.1 does it.
pip freeze lists every installed package with its exact version, and > saves that list as requirements.txt. On another computer, one command makes the same environment:
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: it's big, and it only works on the computer that made it.
Open a notebook
JupyterLab: start it from the project folder, with the environment active:
(.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. Keep the terminal open while you work; pressing Ctrl+C in it stops JupyterLab. Click Python 3 (ipykernel) in the launcher to make a new notebook, and run a cell with Shift+Enter.
VS Code: with its Python and Jupyter extensions installed, open the ml-course folder, make a file called first.ipynb, click Select Kernel at the top right, and choose the Python in .venv.
Either way, check you've got the right Python by running these two cells. This page runs them too: on your own computer the versions will be whatever pip installed, probably newer.
Wrong way: installed, but not found
The most common setup problem: you install NumPy, open a notebook, type import numpy, and get ModuleNotFoundError: No module named 'numpy'. You did install it. But you installed it into one Python, and the notebook's kernel is another (step 1 showed how that happens).
Run the cells below to see the error for yourself:
-
This page’s Python has NumPy, pandas and matplotlib, but not scikit-learn yet: it arrives in Stage 2. So here,
import sklearnfails exactly the wayimport numpydoes on a computer where NumPy was installed into a different Python from the notebook’s. -
-
To fix it on your computer, check two things:
- 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. - Where did
pipinstall? Activate the environment first, and usepython -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
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.
- Python Packaging User Guide: Install packages in a virtual environment using pip and venv: making, activating and installing into a virtual environment, and requirements files
- The Python Standard Library: venv: what a virtual environment is
- Jupyter: What is Jupyter?: notebooks, kernels and the apps that open them
- JupyterLab: Installation: installing and starting JupyterLab
- VS Code: Jupyter Notebooks in VS Code: opening notebooks and choosing a kernel
- scikit-learn: Installing scikit-learn: pip install scikit-learn, in a virtual environment