Is Python Compiled, Interpreted, or Both?

A common question: “Is Python compiled, Interpreted, or both?”.

Is Python Compiled, Interpreted

Types of Programming Languages

Any high-level programming language convert to machine language in two ways as

  1. Compilers
  2. Interpreters

Compilers :

Convert high-level program to its machine or CPU instruction sets I,e machine bytecode. Therefore, the compiler checks for its syntax first and convert the whole program to machine or CPU understandable bytecode.

compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code). And interpreters are step-by-step executors of source code, where no pre-runtime translation takes place.

Ex: c, c++, Java

Interpreters:

Interpreters work differently; they take each expression or line of the program and convert to machine code and execute it. Hence, if there is an error in a specific line, it’ll be shown at the time of execution.

An interpreted language is a type of programming language for which most of its implementations execute instructions directly and freely. Without previously compiling a program into machine-language instructions. The interpreter executes the program now, translating each statement into a sequence of one or more subroutines and then into another language (often machine code).

Ex: Unix shell

Is Python Interpreted?

It is a bit strange in terms of compilation, coming to Python, remember this word.

Python is a “COMPILED INTERPRETED” language.

This means when the Python program is run,

  1. First Python checks for program syntax
  2. Compiles and converts it to bytecode, and directly bytecode is loaded in system memory.
  3. Then compiled bytecode interpreted from memory to execute it.

Whereas other languages like c convert programs to machine code and save them as executables in the disk. And then the user can run it as a.out

We usually call Python an interpreted language because the compilation happens behind the scene and when we run the python code through:

python Hello.py -> directly executes the code, and we can see the output provided that code is syntactically correct

python Hello.py -> it looks like it directly executes, but it first generates the bytecode that the interpreter interprets to produce the native code for the execution purpose.

CPython takes the responsibility of both compilation and interpretation.

Proof for Python compilation is when you import any Python module in another program. In the imported module directory, another file with the same name .pyc will generate. This file executes later on.

Though some believe and say Python is an interpreted language, it is not. The compilation happens behind the scene, and when we run the python code through the terminal, it gets converts a compiled file.

Must Read

5 Incredible Uses of Numpy Shuffle With Examples
Matplotlib Colorbar Explained with Examples
Python float to string Conversion Using 10 Different Methods

Conclusion

To summarize, Python is an interpreted language, unlike other programming languages. All the errors are displayed when the specific line containing the error is compiled.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Stevo Devo
Stevo Devo
2 years ago

Java works the same way. The javac command compiles to bytecode and then the bytecode is interpreted by the JVM.

Last edited 2 years ago by Stevo Devo