Bigger programs · lesson 9 of 12
Parts in this lesson
Apython file.pyruns a script
B>>>the interactive prompt
Cpython -m venv .venva project's own Python
Dpython -m pip installadds a package
Eruff formattidies the layout

Python on your computer

Last updated

So far every program has run in this page. Real projects run on your own computer, from files you keep. In this lesson you'll install Python, run a script from a terminal, try the interactive prompt, give a project its own virtual environment, install a package into it with pip, and tidy your code with ruff.

By the end of this lesson you can

  • Install Python and run a script from a terminal
  • Make a virtual environment for a project, turn it on, and install a package into it with pip
  • Explain a ModuleNotFoundError for a package that is installed somewhere else
  • Format and check your code with ruff

Install Python

Python comes from python.org. This course uses Python 3.14, and any newer 3.x works too.

  • Windows: install the Python install manager from python.org/downloads or from the Microsoft Store. After it's installed, the python command starts Python, and fetches the latest version the first time if none is installed yet.
  • macOS: download the installer from python.org/downloads and run it. On a Mac the command is python3.
  • Linux: Python 3 is usually there already. Use your system's package manager if it isn't. The command is python3.

Then open a terminal (on Windows, Terminal or PowerShell; on a Mac, Terminal) and check it worked:

> python --version
Python 3.14.2

On macOS and Linux, type python3 wherever this course says python. The exact version number you see will differ.

On a Mac, which command starts the Python you installed from python.org?

Run a script

A script is a Python program saved in a file ending in .py. Make a folder for it, say library, and save this as report.py inside. Here it runs in the page:

Step 2 code report.py
Edit the code, then run it again

In the terminal, move into the folder with cd, then give python the file's name:

> cd library
> python report.py
Dune 75
Emma 0

Same program, same output. Every example in this course runs this way too: save it as a .py file and run it.

You're in the folder that holds report.py. What do you type to run it?

The interactive prompt

Type python with no file name and Python starts its interactive prompt, often called the REPL. It shows >>> and runs each line as soon as you press Enter, then shows the result. You don't need print to see a value:

> python
>>> 3 * 25
75
>>> title = "Dune"
>>> title.upper()
'DUNE'
>>> exit()

exit() (or Ctrl+D on macOS and Linux, Ctrl+Z then Enter on Windows) leaves the prompt. It's the quickest way to try a line or two, but nothing you type is saved: put anything you'll want again in a file.

In the interactive prompt, you type len("Emma") and press Enter. What does Python show?

An editor

You can write Python in any plain-text editor, but a code editor helps a lot: it colours the code like this page does, points out mistakes as you type, and runs the file for you. Visual Studio Code with its Python extension is free and popular. IDLE, the small editor that comes with Python, is enough to start with.

Whatever you use, keep each project in its own folder, and open the folder (not just one file) in the editor. Your terminal and your editor then agree on where the files are.

Why keep each project in its own folder?

A virtual environment and pip

Python comes with a large standard library, and thousands more packages are on the Python Package Index (PyPI). You'll add them with pip, Python's installer.

Install packages into a virtual environment, a folder holding a private copy of Python just for this project, rather than into the Python every project shares. Projects then can't clash over versions, and you can delete the folder and start again at any time. Make one called .venv inside the project folder:

> python -m venv .venv

Then activate it, so that python in this terminal means the project's Python. On Windows (PowerShell):

> .venv\Scripts\Activate.ps1
(.venv) >

On macOS and Linux:

$ source .venv/bin/activate
(.venv) $

The (.venv) at the start of the prompt shows it's on. Now install a package. isbnlib checks and tidies book numbers (ISBNs):

(.venv) > python -m pip install isbnlib
...
Successfully installed isbnlib-3.10.14
(.venv) > python check_isbn.py
True
9780441013593

Writing python -m pip, rather than just pip, makes sure the package goes to the Python you're running. deactivate turns the environment off again. Most editors notice a .venv folder in the project and use it.

What does the (.venv) at the start of the prompt tell you?

Installed, but not here

check_isbn.py worked above. Now open a new terminal, where the environment isn't active, and run it again:

> python check_isbn.py
Traceback (most recent call last):
  File "check_isbn.py", line 1, in <module>
    import isbnlib
ModuleNotFoundError: No module named 'isbnlib'

This page's Python has no packages installed either, so it does the same. Press Run:

Step 6 code check_isbn.py
Edit the code, then run it again

ModuleNotFoundError: No module named 'isbnlib'. The package is installed, but only inside .venv. Every Python has its own packages, and a fresh terminal runs the Python everyone shares, which doesn't have it. The fix isn't to install it again everywhere: activate the environment (step 5) and run the script again.

For the page, here's the same check written with only what comes with Python:

Step 6 code main.py
Edit the code, then run it again

Tidy code with ruff

This program works. Press Run: it prints the same as report.py in step 2.

Step 7 code report.py
Edit the code, then run it again

But it's hard to read: the spacing changes from line to line, and it imports math without using it. ruff is a tool that fixes both. Install it into the environment and check the file:

(.venv) > python -m pip install ruff
(.venv) > ruff check report.py
F401 [*] `math` imported but unused
 --> report.py:1:8
  |
1 | import math
  |        ^^^^
help: Remove unused import: `math`

Found 1 error.
[*] 1 fixable with `--fix`.

ruff check is a linter: it finds code that runs but is probably a mistake. --fix fixes what it safely can. ruff format is a formatter: it rewrites the layout to the style PEP 8 describes, and doesn't change what the code does.

(.venv) > ruff check --fix report.py
Found 1 error (1 fixed, 0 remaining).
(.venv) > ruff format report.py
1 file reformatted

Afterwards report.py is exactly the program from step 2. Most editors can run ruff format each time you save, so you never have to think about spacing again.

What does ruff format change?

Check what you learned

Three questions. Pick an answer, then check it.

Why give each project its own virtual environment?
You installed isbnlib into .venv yesterday. Today, in a new terminal, import isbnlib gives ModuleNotFoundError. What should you do?
Which command finds code that runs but is probably a mistake, like an import you never use?

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 Python