Skip to content

Can Python be compiled? is it compiled or interpreted?

Can Python be compiled or in other words: is Python compiled or interpreted language?

To save you a few minutes the answer is straightforward which is both: python is compiled as well as interpreted language. But hold on, when you read some books or when you read some articles on internet, they tell you that python is an interpreted language. Why so? and why I’m saying Python is compiled or integrated language? Let’s find out in this article

Yes, Python is compiled and interpreted language

So let’s see why Python is both compiled and interpreted language. First let’s find out what is a compiler or when do you say a language is a compiled language: we have so many programming languages like C++ Python, Java and many more. When it comes to C and C++ they are compiled languages because you, as a programmer, you write a C program here is C code, these are high level languages. This simply means your machine cannot understand these languages. because by default computer works only on machine language. This means when you write a code is in C language you have to convert that code into a machine language and that’s where compiler comes to the picture.
Compilation process for C based Languages
Compilation process for C based Languages

What is the exact role of a compiler?

C code gets compiled to give you the machine code the same goes for other language as well, so that’s why we say Python is a compiled language. A compiler don’t just have a way of converting a high-level language into machine language. Sometimes you also convert a language from A to B where A can be a language and B can be a language provided there’s a way to convert that. So a compiler can convert any language from any language. You cannot simply say from high to low it can be from high to high as well that’s the basic term which we use compiler. Basically when you work with Python, we have a different way of working: the moment you work with Python file its first gets compiled
Python compilation process
Python compilation process
You read it right, it first gets compiled to give you a bytecode then that bytecode will be interpreted in a machine language. who does that? it’s the special virtual machine. You might be interested to read this article: What Python is used for? But hold on why this complex process? and what exactly this interpreted means? Basically interpreted languages simply means you have a set of instructions and the interpreter will read each and every line one by one then it will start executing everything line by line. That’s why it is called interpreted language. Now your bytecode is interpreted. why we have to make it so complex of having comparison first and then going for interpretation? I heard you asked; the answer is only for one reason we use a concept of bytecode to achieve portability. Let me explain: this term; portability; simply means independence that you can write code once and you can run on different platform.

Can Python be compiled? Yes, but Readability Counts…

For example, as your machine changes your CPU architecture also changes. If you’re writing a code for one machine and if you’re compiling it to get a binary file or a native code it may not work on some other machines, because machine may have a different CPU architecture. To solve this problem, we have a concept of virtual machine. In this virtual machine what you run is a bytecode then this bytecode will run on that virtual machine and that will convert into native code. Tthat simply means regardless of the machine and CPU architecture, so if your machine has a VM software or the virtual machine software you’re good to go. When I say virtual machine, are you thinking about VMware or something. Not exactly we are not trying to copy a physical hardware, this virtual machine which I’m talking about is very specific to the languages. For example, in Python we have PVM (Python Virtual Machine), the same concept is true in Java as well which is a JVM so we have virtual machine in Java which is JVM and for Python we use PBM.

Python Virtual Machine (PVM)

On JVM we can run other language as well like Scala, groovy and Kotlin; but on PVM we can run only Python codes. Basically you write a Python code which gets compiled which is the bytecode. That bytecode will be interpreted on your PVM to give you the actual output then the question that arises is: when you are running Python you never did that? because if you are coming from Java background by any chance in Java we have this concept that first you have to compile the code, whereas in Python we don’t do that. We simply say Python then we mention the filename. we are not doing compilation, and that’s the beauty of Python. Python says hey programmer you don’t have to worry about compilation, I will take care of that. You simply say Python and the file name I will compile it and I will give you the bytecode and then that bytecode will run on your PVM. What about interactive shell? so normally when we use REPL, we directly write code to the command line, for example we simply say print hello, is it creating the bytecode as well? The answer is: yes Behind the scene whatever you do and Python, it creates a bytecode first and that byte code gets run on PVM. PVM is very important in terms of let’s say if you want to do performance tuning later, or if you want to understand how exactly everything is working behind the scene.

Other Python Implementations..

There is one more thing which we have to address here and which is CPython, that’s weird right what is the CPython? what if I say whatever we have learned in the Python series everything was about CPython. But we have never mentioned that because normally we always talk about Python language, we never talk about the implementation. What if I say python is just a language, a set of rules and conventions; the actual implementation which we are using here is CPython. Whatever we have learned is actually CPython we have some other implementation for Python as well.
Dosa Dish
A dosa dish
For example if you want to eat in a dish, let’s say if you want to eat dosa, you have a concept in mind of how dosa looks like. Then if you go to different restaurants they have a different way of serving it and now we have different versions of dosa. In the same way for Python is a language and then we have different implementation we have CPython, PyPy, IronPython and Jython. Basically when you talk about CPython it is implemented in C language. That means that the internal working is happening with C language. On the other hand when you say Jython, which is Java somewhere, the implementation is done using Java language, then we have the dotnet version of it which is IronPython which is the dotnet implementation of it. There is one language and we have different implementations but in general the widely used implementation is CPython.

A Piece Of Advice

I would also suggest you to have a look on this other stuff as well . Just to give you a summary whatever we have discussed till now what is Python its it’s a compiled and interpreted language. Even if you are not compiling it, behind the scene it’s happening so it is getting compiled. I hope you enjoyed it, please let me know you questions in the comment section and follow me on twitter for your daily Python update.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)