DEV Community

Jessica Alves
Jessica Alves

Posted on

A short explanation of compilers and interpreters in Python code


Compilers and interpreters are part of the implementation of a programming language, and compilation and interpretation are two stages that make up the code execution process. This article is about compilers and interpreters in Python context.

What is a compiler?

It's a program that takes your code (the code you, human, wrote it), a high level language, and turns it into another language, in Python's case, into bytecode (or a lower level language). Compilers are going to make sure the syntax of your code is correct, that it's formatted properly, that Python's indentation is perfect. Once all checks are done, that means the code is translated and compilers are making sure the code is ready to be run and executed by the interpreter.

And what is an interpreter?

It's a program that will read the compiled code and execute it line by line. In summary, the interpreter is also converting the compiled code into a language that CPUs can understand so that your code can be executed and run by the machine. It translates code into target machine code during execution. Or in simple words, the interpreter is enabling your code to be run.

Compilation errors x runtime errors

Compilation errors are errors that happen during the compilation stage, the first stage I described above. That means while the compiler was trying to "translate" your code to bytecode it found an error, e.g. syntax errors, formatting errors or indentation errors, so it crashes.

The runtime errors happen during the interpretation stage, the second stage I described above. They are errors that happen during the execution process of the code, e.g. errors in the logic of a function, or logic errors in an if statement, or zero division errors, or type errors (trying to divide an integer by a string), etc.


The process described above is usually what happens for most modern languages, including Python. You can find some differences like in Java ou C++, where the compilation stages are much more complex and advanced than in Python. That's why you hear people saying that Java and C++ are compiled languages and Python is an interpreted language. That doesn't mean Python doesn't have a compilation step, but in Python most of the effort is being done during the interpretation stage.

Top comments (0)