Skip to content

Interpreter

Also called: interpreters

Published:

How does an interpreter differ from a compiler?

The Python documentation calls Python an interpreted language rather than a compiled one: source files can be run directly, without explicitly building an executable first [1]. Interpreted languages, it says, typically have a shorter development and debug cycle, while their programs generally run more slowly [1].

The line is not sharp. Python has a bytecode compiler inside it, and the documentation openly says this blurs the distinction [1]. Python source is first compiled to bytecode, which then runs on a virtual machine [1].

How do you use the Python interpreter?

The interpreter behaves rather like the Unix shell. Connected to a terminal, it reads and executes commands one at a time; given a file name, it reads and runs the script in that file [2]. In interactive mode the primary prompt is >>> and continuation lines get the secondary prompt ... [2].

A concrete example: type python with no arguments, enter 2 + 3 at the prompt and 5 comes straight back. The documentation describes this interactive use as a powerful way to try out new ideas and inspect modules [1]. Running python -i script.py executes the script and then drops into interactive mode, which is handy for poking at the variables it left behind [2].

Why can interpreted programs be slower?

Code is translated and executed on every run, which adds overhead. Python trims some of that by caching bytecode [1]. The documentation's general verdict still holds, though: the short edit-and-run cycle usually comes at the price of slower programs [1].

Frequently asked questions

How do you exit the Python interpreter?

Press Control-D on Unix or Control-Z on Windows, or type quit().

Does an interpreted language never compile?

It can. Python compiles source to bytecode first, which is why the interpreted versus compiled distinction is blurry.

Sources

  1. Glossary — Python documentation
  2. 2. Using the Python Interpreter — Python documentation