Back to articles list Articles
8 minutes read

What Is the Python Interpreter?

Without an interpreter, your Python code is just words on a screen. Find out what a Python interpreter is, what it does, and what your code looks like ‘under the hood’.

At the most fundamental level, computer programs are simply sets of instructions. To do some basic arithmetic and save the output we need something like the following instructions: ‘load the number 8’, then ‘load the number 9’, then ‘add the first number to the second and store it somewhere’. But programs are typically written at a higher level in human-readable code like Python. So how does the computer go from this human-readable code to a list of instructions it can execute? That’s where the Python interpreter comes in.

You don’t necessarily need to know anything in this article to be a successful Python programmer, but it’s nevertheless interesting to know how Python takes the code you write and executes it. It could, however, be useful in a practical sense to understand how your programs are running and why some implementations may be faster than others. We’ll show you a nice example to illustrate this later.

Before you go further here, we recommend reading Python Terms Beginners Should Know – Part 1, where we define some important terms. And if you’re new to programming and want to find out if it’s for you, consider taking our Python Basics track. It includes three interactive courses that teach you the fundamentals of programming with Python.

How Programs Are Run

Inside your computer, there is a Central Processing Unit (CPU) and memory in the form of Random Access Memory (RAM). The RAM temporarily stores the instructions of a program and the CPU fetches and executes them. The instructions stored in RAM are referred to as machine code and are processor dependent (i.e. the machine code will be different for different processors).

The Python source code you write is stored as a text file on the hard drive. However, the CPU in your computer doesn’t natively understand Python – or any other programming language, for that matter. It only understands machine code, which is represented as binary digits. So, the problem boils down to getting the human readable code translated into the machine code that the processor can understand and then execute.

Compiled vs. Interpreted Languages

There are two approaches to solve the problem of translating the code we write into a set of instructions the processor can understand and execute. We can illustrate this with an analogy. Suppose you’re an avid reader who’s in the mood for some mind-bending literature from Franz Kafka. But if you can’t understand German, a book written in German won’t get you far. Your two options are to find someone to translate the whole book into English or get a German-speaking friend to read the German version line-by-line and verbally translate (or interpret) it into English for you.

The same is true for computers. Compiled languages like C or C++ take the first approach. The translator in the above analogy is a computer program called a compiler. The compiler takes the source code you write as input and translates all of it into instructions in machine code.

Interpreted languages take the second approach. As the program is running, the interpreter reads the source code line by line and translates it into instructions for the processor.

The process of having an interpreter translating code at runtime creates some computational overhead. Therefore, interpreted programs are typically slower than programs compiled directly into machine code. Compiled code, on the other hand, needs to be translated for a specific processor and is therefore platform dependent.

So, is Python compiled or interpreted? Like most things, it’s a little complicated. Python is both compiled and interpreted, depending on how you use it. It can be run as an interpreted language in interactive mode, or it can compile your source code to a binary and then execute the binary. We’ll go into the details below.

The Python Interpreter

Let’s start by assuming you already have downloaded and installed Python and have a script ready to go. (If you need help with this, check out How to Install Python on Windows.) When you want to run a script called ‘script.py’ from the command prompt, you need to first open the command prompt and navigate to the directory where the script is saved. Then you can type …

	python script.py

.. and hit Enter. (Need more details? Our article How to Open and Run Python Files in the Terminal has them.)

Typing the word python into the command prompt invokes the Python Interpreter, which is the program you are actually running. It simply takes your script (as the argument) to be processed further.

The Python Interpreter itself consists of two parts: a compiler and the Python Virtual Machine (PVM). The compiler does what compilers do; it translates the source code in script.py into something. This something, however, is not machine code. It’s an intermediate format called bytecode, which also happens to be represented in a binary format. If you’ve ever noticed some .pyc files floating around in a project directory, this is the bytecode. It cannot be understood by the processor and is hence not processor dependent. It’s meant to be understood by the second component of the Python Interpreter - the Python Virtual Machine.

The Python Virtual Machine is a piece of software that does a similar job to the physical processor. It reads the bytecode and executes the instructions.

How to Use the Python Interpreter

We’ll explore two ways to use the Python Interpreter: first running it interactively and second passing it a Python script. To open the Python Interpreter in interactive mode, open the command prompt and type:

	python

After you hit Enter, the interpreter prints a welcome message with some system information and Python version information. From here, you can simply start typing your program where you see the prompt (>>>). Try printing ‘Hello, World!’.

For multi-line code – e.g. a for loop – the indented code starts from the secondary prompt (...). To execute your code all you need to do is hit Enter.

In the interactive mode, the code is executed immediately, with the results shown directly below. For more information and examples, see the Python documentation on Using the Python Interpreter. Once you’re finished working in interactive mode, type exit() or quit() to go back to the command prompt.

For the second method, you’ll need to create a simple script like the hello world program. Just open a text editor and type:

print('Hello, World!')

Then save this as script.py. To run this script, once again you’ll just open the command prompt or terminal and type:

python script.py

After you hit Enter to execute the program, you should see the output printed directly to the command prompt. Just note that while it’s possible to write and execute programs in this way, it’s not the most convenient. Programmers mostly use IDEs to write and run programs. We have an article on the 4 Best Python IDE and Code Editors with more details.

A Closer Look at Bytecode

In the introduction, we promised all this talk about interpreters and compiling code could be of practical use. So let’s inspect the bytecode of two functions and see the instructions being passed to the Python Virtual Machine for execution. These are in the binary .pyc files we mentioned above. We can’t make sense of the binary code, but we can get a human-readable version of the instructions by using the dis module. This allows us to see what Python is doing ‘under the hood’ when it’s running our code.

This can be specified at the command prompt when calling the Python Interpreter:

	python -m dis script.py

For this exercise, we want to compare two slightly different implementations of the same function. In the Python Interpreter, we can define our functions that simply add two numbers:

	>>> def add_numbers_v1():
	...     return 8 + 9

	>>> def add_numbers_v2():
	...     a, b = 8, 9
	...     return a + b

If you time these functions, you’ll notice the second implementation is much slower. We can understand why by importing the dis module to disassemble our two functions and inspect the instructions:

>>> import dis
>>> dis.dis(add_numbers_v1)
      2           0 LOAD_CONST               1 (17)
                  2 RETURN_VALUE

>>> dis.dis(add_numbers_v2)
      2           0 LOAD_CONST               1 ((8, 9))
                  2 UNPACK_SEQUENCE          2
                  4 STORE_FAST               0 (a)
                  6 STORE_FAST               1 (b)

      3           8 LOAD_FAST                0 (a)
                 10 LOAD_FAST                1 (b)
                 12 BINARY_ADD
                 14 RETURN_VALUE

Each line in the output is an instruction. The first and faster version contains only two instructions, compared to the eight instructions for the second implementation. This explains the difference in execution time. If you ever find yourself wondering what Python is doing behind the scenes, look at the bytecode to get some insights.

Put That Python Interpreter Into Use: Learn Python!

We covered a lot of conceptual material in this article, giving you an overview of the Python Interpreter with some practical examples. If you’re interested in how Python was developed, read A Brief History of Python, which will provide a little more context.

The only thing left to do now is to learn Python. After all, it is one of the most popular programming languages and is quite beginner friendly, like we discuss in Why Python is a Good Programming Language. Happy learning!