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 articleYes, 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.
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
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.
Like!! Thank you for publishing this awesome article.
Wow! Thank you! I continuously needed to write on my site something like that. Can I include a part of your post to my site?
We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.