Sub topic 10
Running a script from the terminal
This chapter assumes you have verified that Python 3 is installed and that you can open a terminal. It also assumes you have saved a file named hello.py containing one line. You will run that file, learn what the interpreter does with it, and learn why the directory you run it from matters.
The basic command
Open a terminal and change into the folder that holds hello.py. Then pass the file name to the interpreter.
python3 hello.py
# On Windows, use: py hello.pyThe file contains this:
print("Hello, world!")The terminal prints Hello, world! and returns to the prompt. The interpreter has exited, so no Python session stays open. This differs from the REPL, where each line runs as soon as you type it and the session stays open.
What happens when you run a file
The command python3 hello.py starts a new interpreter process and gives it the file name. The interpreter reads the whole file and compiles it to bytecode, which is a compact internal form. It then executes the bytecode from top to bottom and exits.
Because Python compiles the whole file first, a syntax error anywhere in the file stops the run before any line executes. A runtime error, such as a misspelled variable name, only appears when execution reaches that line. Earlier lines will already have run.
The working directory
A terminal always has a current working directory. When you give python3 a relative path such as hello.py, Python looks for the file in that directory. If the file is elsewhere, you get an error like this:
python3: can't open file '/home/ada/hello.py': [Errno 2] No such file or directoryYou have two ways to fix this. You can change into the correct folder with cd, or you can pass a path to the file.
cd projects/first-steps
python3 hello.py
# or, from anywhere:
python3 projects/first-steps/hello.pyUse pwd on macOS and Linux, or cd with no arguments on Windows Command Prompt, to print where you are. Use ls (or dir on Windows) to confirm the file is listed.
Running with a different interpreter
If several Python versions are installed, the command you type decides which one runs the script. You can check which interpreter a script will use by printing its version from inside the script.
import sys
print(sys.version)
print(sys.executable)sys.executable shows the full path of the interpreter that is running the file. This is helpful after you create a virtual environment, because it tells you whether the environment's interpreter is active.
Output and exit status
Output from print goes to standard output, which is your terminal by default. You can redirect it to a file with >.
python3 hello.py > output.txtWhen a script ends normally, it returns exit status 0. An uncaught error returns a non-zero status, usually 1. Other programs and shell scripts use this status to decide whether the run succeeded. On macOS and Linux, echo $? prints the status of the last command. On Windows Command Prompt, use echo %ERRORLEVEL%.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| `No such file or directory` | Wrong working directory or file name | Use `pwd` and `ls`, then `cd` or give a full path |
| The REPL opens instead of running | You typed `python3` with no file name | Add the file name after the command |
| `SyntaxError` before any output | A syntax mistake somewhere in the file | Read the line number in the message |
| Editor "Run" works, terminal fails | Different interpreter or directory | Print `sys.executable` in both |
Exercise
You run python3 report.py and see can't open file ... [Errno 2] No such file or directory. The file exists in your Documents folder. Name two things to check, and say which command shows you where the terminal currently is.
Show answer
Check that the terminal's working directory is the folder containing report.py, and check that the file name is spelled exactly right, including the .py extension. The command pwd on macOS and Linux shows the current directory. Running cd with no arguments does the same on Windows Command Prompt. You can also run the script with its full path.