Is Python Compiled, Interpreted or both?

A common question: “Is Python compiled, Interpreted or both?”. Usually, the asker has a simple model of the world in mind, and as is typical, the world is more complicated.

Overview

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 to do this compiler checks for its syntax first and convert 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 not interpreters (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 program and convert to machine code and execute it. Therefore if any syntax error in some line of interpreted language, you will get error only when that line is encountered.

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 directly, translating each statement into a sequence of one or more subroutines, and then into another language (often machine code).

Ex : Unix shell

Then……..what about Python?

Coming to Python , it is bit strange in terms of compilation , remember this word.

Python is a “COMPILED INTERPRETED” language.

Means when 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 converts programs to machine code and saves them as executables in disk and then user can run it as a.out

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

python http://Hello.py ——-> directly excutes the code and we can see the output provied that code is syntactically correct

python http://Hello.py it looks like it directly executes but really it first generates the bytecode that is interpreted by the interpreter 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 , then in imported module directory another file with same name .pyc will be created which is compiled version of that file.

Though some believe and say Python is an interpreted language, it is definitely not. The compilation happens behind the scene and when we run the python code through and this statement is conclusion for the same.

Leave a Reply

Close Menu