Lesson 13 of 145 min
From a notebook to a script
A script is a file of Python that the terminal runs from the first line to the last; it finds the file in the terminal's folder, and it shows a value only when you print it.
After this lesson you can write code in an editor as a .py file in a project's folder, and run it from the terminal.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
A new terminal starts at home
The editor's terminal starts in the project's folder. A new terminal window starts in your home folder, with no environment active. So first go to the project's folder, then turn on its environment, then run.
cd ~/projects/hub-stats
source .venv/bin/activate
python main.py32180050
On Windows
The video shows a Mac's terminal. On Windows, the editor's terminal is PowerShell, and its line ends in > with the whole path: (.venv) PS C:\Users\you\hub-stats>. cd .. and ls work the same, and Command Prompt lists a folder with dir. The environment turns on with its own command.
cd ~\projects\hub-stats
.venv\Scripts\Activate.ps1
python main.pyIf PowerShell won't run Activate.ps1
On Windows, PowerShell may refuse to run the environment's Activate.ps1 at first. Python's own guide gives one command that allows it for your user. Run it once, then turn on the environment again.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserA data file read by its name
A file name in the code is looked for in the terminal's folder too, not beside the script. Run table.py from the folder above and it stops. Path(__file__).parent is the script's own folder, so a file beside the script is found from anywhere.
from pathlib import Path
import pandas as pd
here = Path(__file__).parent
df = pd.read_csv(here / "penguins.csv")
print(len(df))FileNotFoundError: [Errno 2] No such file or directory: 'penguins.csv'
Code that runs only when you run the file
Taking a name from a module runs the whole module. Many modules end with this line, so their own test runs only when you run that file, and never when another script takes names from it. Here python hub.py prints the downloads, and from hub import downloads prints nothing.
if __name__ == "__main__":
print(downloads("gpt2"))32180050
You will also see uv run
Many projects now use uv. In the project's folder, uv run runs a script in the project's environment, with no need to turn the environment on first.
uv run main.py32180050
Run on Python 3.13.3, requests 2.34.2, pandas 3.1.1 and uv 0.11.15, on a Mac. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
A file total.py holds prices = [12, 8, 20] and, on its last line, sum(prices). In a notebook cell, these lines show 40. What does python total.py show?
2 of 2
The terminal's line ends projects %. python main.py says can't open file '/Users/you/projects/main.py': [Errno 2] No such file or directory. The file is in hub-stats, inside projects. What fixes it?
The ideas in this lesson
- A script
- A script is a .py file. python runs it from the first line in a fresh Python, and shows a value only when you print it.
- The terminal's folder
- A terminal is always in one folder. python looks for the file there, and cd moves the terminal to another folder.
- A module
- A .py file in the same folder is a module: from hub import downloads runs hub.py and takes its function.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
The same two lines
Here's a bet. In a notebook, these two lines ask about an AI model. The cell shows how many times it was downloaded. Now I put the same two lines in a file. And I run that file. What shows up? The number, or nothing?
The bet
Same two lines, same Python. So will the number show? Make your bet.
A file of Python
A notebook is good for trying things. But a program you run again and again, or give to someone, lives in a file. You write it in an editor, an app for writing files of code, like VS Code.
The file sits in the project's folder, hub-stats. This one is main.py, with the same two lines. The end of its name, dot py, says it's Python.
A file of Python like this is called a script.
Run it
So how do you run it? In the terminal, the window where you type commands. The editor has one, and it starts in the project's folder. Its line starts with dot venv: the project's environment is active. You type python, then the file's name. A new Python starts, runs every line from the first to the last, and stops. Every run starts with an empty memory, like restart and run all.
So, your bet. It runs... and the terminal shows nothing.
Only print shows it
If you bet on the number, that's what a notebook does. It shows a cell's last value by itself. A script never does. So what one change makes the number show?
Print. The last line goes inside print's brackets. Run it again, and there's the number.
The file did the same work both times. Only print puts the value on the screen.
The terminal's folder
Python ran the file main.py. So where did Python look for it?
A terminal is always in one folder. Its name is at the start of the line where you type, before the percent sign. Here, it's hub-stats.
The command ls lists what's in that folder: hub.py and main.py. Python looked for main.py in that folder, and nowhere else.
One folder up
Now type cd, then two dots. cd changes the terminal's folder, and two dots mean the folder above. So the terminal moves up, to projects. Then type python main.py again. What goes wrong?
It can't open the file. Look at the path it tried. It ends in projects, then main.py. So Python looked in projects. And ls, run there, shows only one thing: the folder hub-stats.
Type cd, then hub-stats, and the terminal moves back in. Now it runs. That's why the editor's terminal starts in the project's folder.
A module
The script main.py starts with this line. It takes something called downloads, from hub. So where does downloads come from?
From hub.py, in the same folder. It holds a function, made with def. The function asks the Hugging Face Hub about a model, and returns its downloads.
A file like hub.py is called a module. This line runs hub.py. Then it takes that one name, downloads. The module's name is the file's name... without the dot py.
Real AI code
Real AI code is built the same way. nanoGPT is a small program on GitHub that trains a GPT. Its model lives in model.py, and its script is train.py. So how does train.py get the model?
With one line, like ours: from model, take the model's names. And you run python train.py. Two notebooks sit in the same folder too. So a notebook is for trying things, and the script is what you run.
What to keep
So, a script is a dot py file. Python runs it from the first line to the last, and it shows a value only when you print it. Python looks for the file in the terminal's folder. And a file in the same folder is a module the script can take names from.
Try it
A script runs from the first line, in the terminal's folder. Now try it on this lesson's page, and take the quiz. Write hub.py in line one.
Then read what Python says.
You use this lesson in Lab 5: On your own computer, after Git and GitHub.
Facts last checked on .
