Lesson 1 of 149 min
Notebooks and environments
A notebook runs your cells on one Python, which remembers only what you ran and imports only what was installed into it.
After this lesson you can run a notebook in Colab and on your own machine, and install a package.
Loading the video…
In real code
What the video left out, as you will meet it in your own code.
Which Python is this?
The error at the end of the lesson came from two Pythons. A notebook can tell you which one it runs: sys.executable is that Python's place on your computer. If the path has .venv in it, the notebook runs on the project's environment. In a terminal, deactivate takes the window back to the main Python.
import sys
print(sys.executable)/Users/Shared/my-project/.venv/bin/python
On Windows
The steps are the same, with Windows' own lines. PowerShell may refuse to run Activate.ps1, because it doesn't run scripts at first. Python's own guide gives the line that allows your scripts, once for your user. In the older Command Prompt, activate with .venv\Scripts\activate.bat instead.
python -m venv .venv
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
.venv\Scripts\Activate.ps1A wrong guess in Python 3.15
Python 3.15 adds a guess to the error from the lesson. Here the guess is no help: runpy is a part of Python, not the package you wanted. The fix is still the same. Install the package into the Python that ran the line.
import numpyModuleNotFoundError: No module named 'numpy'. Did you mean: 'runpy'?
After an upgrade, restart
A newer version of a package you already imported changes the files, not the memory. So the notebook keeps the old one until you restart, and %pip says so. Restart, then run all. In Colab, that's Restart session and run all.
import numpy
print(numpy.__version__)
%pip install -q --upgrade numpy
import numpy
print(numpy.__version__)2.2.6 Note: you may need to restart the kernel to use updated packages. 2.2.6
In Colab, install in the first cell
On the free plan, a Colab notebook runs for 12 hours at most, and it stops sooner if you leave it idle. Then Colab deletes the computer behind it, and what you installed goes too. So put every %pip line in the first cell. Then restart and run all installs them again for you.
%pip install -q cowsay
import cowsayuv: the same jobs, faster
You'll meet uv in newer projects. It makes the project's folder and its .venv, installs packages, and runs your code on that environment, without an activate step. It also writes down what the project needs, in a file called pyproject.toml.
uv init my-project
cd my-project
uv add pandas
uv run python -c "import pandas; print(pandas.__version__)"3.1.1
Run on Python 3.13.3 and 3.15.2, IPython 9.19.1 and uv 0.13.10. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
Three cells: hours = 2, minutes = hours * 60 and print(minutes). You ran them, and it printed 120. Now you change the first cell to hours = 3 and run only that cell. Then you run the print cell. What does it print?
2 of 2
Your notebook says there is no module named pandas. Which line, in a notebook cell, installs pandas into the notebook's own Python?
The ideas in this lesson
- Notebook memory
- A notebook's cells share one memory, which holds what you ran, in the order you ran it.
- Restart and run all
- Restart and run all empties the memory and runs every cell from the top, as anyone else's run will.
- Environment
- An environment is a folder with its own Python and its own packages, one for each project.
- Installing a package
- pip installs a package into one Python, and only code running on that Python can import it.
Transcript
What the video says, part by part. Press a time to watch from there.
Python for AI
How much Python do you need to read real AI code? Less than you'd think. Hi, I'm Mohi Fayden.
Python is a language for telling a computer what to do. Most AI code is written in it.
This course teaches you the Python you need, and skips the rest. By the end, you'll understand all the code in this series.
It's for you even if you've never written a line of code.
Every lesson has its own page on faydenai.com, with a quiz. There's a lab every few lessons, and a project at the end.
The puzzle
Here's a puzzle. People write and share a lot of AI code in notebooks. Here are three small blocks of code. One sets the hours, one works out the minutes, one prints them. Now I change the hours, and run only that first block. Then I run the last one. Does it print the new minutes, or the old ones?
Colab
Keep your guess. We'll find out soon. First, where can you run code like this, with nothing to install? In Colab. It's a notebook in your browser, and it's free. Python is ready there, with the packages most AI code uses. A package is code other people wrote, ready for you to use.
A page of cells
How does a notebook run your code? A notebook is a page of small blocks like these. They're called cells. You run one cell at a time. Press its play button, or Shift and Enter. What it prints shows up right under it. Run the three cells. One, two, three.
And under the last one: one hundred and twenty.
One memory
But how does the second cell know what hours is?
Every cell you run works on one memory. They all share it. The first cell put hours into that memory. The second cell read hours from it, and put minutes there. The third cell read minutes. Colab shows this memory too, in its Variables panel.
One memory, shared by every cell.
Only when you run it
Now, the puzzle from the start. Change the first cell to three, and run it again. Look at the memory. Hours is three... and minutes is still one hundred and twenty.
Now run the last cell. One hundred and eighty, or one hundred and twenty?
One hundred and twenty. The minutes cell hasn't run since you changed hours. A cell runs only when you run it.
If you said one hundred and eighty, maybe you were thinking of a spreadsheet. A spreadsheet works out every cell again by itself. A notebook doesn't.
Run it again
So run the minutes cell again, then the print cell. One hundred and eighty.
The memory holds what you ran... not what the page says.
A shared notebook
Here's a notebook someone shared with you. Three cells. And under the print cell, one hundred and twenty. It ran for them. But look: the cell that makes hours is at the bottom.
Run every cell, from the top. What happens?
It stops, on the very first cell. Python says hours is not defined.
So why did it work for them? The cells share one memory. They ran that bottom cell earlier, while they worked. So their memory had hours in it. Yours starts empty.
Restart and run all
So how do you catch this before anyone else does? A restart empties the memory. In Colab, it's called restart session. The session is the Python behind your notebook. Then run all goes through every cell, from the top.
Restart, then run all. That's the same fresh run anyone else gets. So you see the problem first.
Move the hours cell up. Restart, and run all. One hundred and twenty.
Before you trust a notebook, or share one: restart and run all.
Many notebooks
So how often does this happen?
Researchers took more than eight hundred thousand notebooks from GitHub. GitHub is a website where people share code. Then they ran them again.
About one in four ran without an error.
The second most common error was a name Python didn't know. That's the one you just saw. The most common was a package that wasn't installed.
So, Colab or your own computer? For this course, Colab is enough. But one day, your own projects will run on your own computer. And that's where you'll meet the most common error.
One Python
On your own computer, you get Python from python.org. Your packages go into that Python. But can one Python hold the packages of every project?
Here are two projects.
An old one was written for pandas two, a package for tables.
A new one needs pandas three.
Install pandas three for the new project. Which pandas does the old project get now?
Pandas three. One Python holds one version of each package. So the old project now runs on a pandas it wasn't written for.
An environment
So each project gets a Python of its own. Open the terminal, the window where you type commands. In the project's folder, one line makes a new folder, called dot venv. It has its own Python, and its own packages.
That folder is called an environment. Each project keeps its own pandas.
Activate, then install
A project's environment is a folder called dot venv, with its own Python. So how do packages get into it? First, you activate it, in a terminal window. That window now starts each line with dot venv. A new window starts without it. Keep an eye on that dot venv. You'll need it in a minute.
Now pip, the installer that comes with Python, puts packages into dot venv. Here it installs Jupyter, the notebook app for your own computer... and NumPy, a package for numbers.
A notebook on it
Then the line "jupyter lab" opens a notebook. It runs on this environment's Python. Import NumPy. That loads it into the notebook's memory... and it works.
Other tools do the same jobs. You'll meet one called uv, which is faster.
The next day
The next day, you open a new terminal window, and you install pandas. "Successfully installed."
Back in your notebook, you import pandas. And Python says there's no module named pandas. Here, module is Python's word for the package.
Why?
Look at the terminal.
No dot venv. This new window runs your computer's main Python, the one from python.org. So pip put pandas there. The notebook runs on the environment's Python... and that one still doesn't have pandas.
Percent pip
So how do you install into the right Python? From the notebook itself. Start the line with a percent sign: percent pip. Percent pip always installs into the notebook's own Python. Run it... and import pandas works.
In Colab it works the same way, for a package Colab doesn't have yet. You'll also see pip with an exclamation mark. That one can run the wrong pip, and put the package in the wrong place.
So in a notebook, install with percent pip.
One Python, two habits
So here are the two habits. The memory holds what you ran, so restart and run all before you trust a notebook. The environment holds what you installed, so install from inside the notebook.
One Python
Your notebook runs on one Python. It knows only what you ran, and what you installed. Now try it on this lesson's page, and take the quiz. Make the notebook say that minutes is not defined.
You use this lesson in Lab 1: Score a chatbot's answers, after Lists, loops and if.
Facts last checked on .
