Is Python Code Compiled Or Interpreted?
If you have read about Python, you might have read that “Python is an Interpreted language”. This is what a common saying is. It is written in many books, and also, in many articles that you would read. But, the surprise is that Python is actually both compiled and interpreted language.
Well, I know that this might be a surprise for many, but yes, Python is both compiled and interpreted language. This article is a discussion of the “Is Python Code Compiled Or Interpreted?”. As you go on reading this article, you would find why Python is both Compiled and interpreted language.
Is Python Code Compiled Or Interpreted?
Well, if you have been into programming for some time, you might have heard that there are some compiled programming languages, like C, and C++, and there are some interpreted languages, like JavaScript.
When we think of some compiled programming language, this is the brief journey of some program from source code to machine code –
- We write the source code(source code is nothing but the code written by us).
- We make use of the compiler, which helps convert your source code into machine-understandable code.
- Note that for different computer architectures and platforms, you would need different compilers.
- Once compiled into machine language, you need neither the source code nor the compiler.
On the other hand, when we think about some interpreted language, this is something that happens –
- We write the source code(again, source code is the code written by us)
- The interpreter executes the instructions line by line, without compiling them to machine language first.
So, when we write and execute our Python program, we can experience that we are directly able to execute the program, and just have a look at the output, but still, Python is both compiled and interpreted language. Python combines the elements of both. This is a glimpse of what happens.
- We write the source code.
- When we make use of the interpreter, it compiles the source code into the “byte code“.
- The byte code is independent of what machine are you using.
- The interpreter has something called a “Virtual Machine”, which understands how to execute the byte code.
- So, the Python code can be compiled to a .pyc file, and then run the compiled version. (this file is there in the __pycache__ folder)
- But the thing is that the code doesn’t run directly. It would still need the interpreter to execute the byte code.
So, Python is both compiled and interpreted. The compilation is often hidden from the programmer, the byte code is internally generated. The compilation step happens automatically when the program is executed for the first time. The benefit is that if the bytecode is up to date, then the bytecode is loaded instead of recompiling the source code.
In summary, when we execute the program, by default, the bytecode is stored in the __pycache__ folder.
Proof for Python is both compiled and interpreted
Is there any proof for Python being both compiled and interpreted or we are just talking about some theoretical stuff? Well, let’s prove this as well!
- Let’s say you are in some folder, and you create a Python file.
- Then you try to execute the Python file through your terminal.
- When you move back to your folder where you have the Python file, you can see the __pycache__ folder.
- If you try to open the __pycache__ folder, you would find the bytecode as a .pyc file in the folder. Even if you execute the .pyc file in place of your python file, you would get the same output.
So, you can see that the __pycache__ folder has the bytecode for the Python program. This tells us that Python is both, compiled and interpreted.
Conclusion –
As you can see, Python is both compiled and an interpreted language. You can observe this from the bytecode. Just the thing is the compilation happens automatically, and then when we are executing the Python program, we just feel as if the program is interpreted, but actually, the source code is first converted to byte code, and then the bytecode is executed by the interpreter.
If you find this tutorial useful, you can consider exploring our other tutorials related to Python, and more articles related to Machine Learning. You can also explore more about Python programming language here!
FAQs related to Is Python code compiled or interpreted?
Q: What is Python?
Ans: Python is a general-purpose, high-level, Object Oriented, Interpreted programming language. It is one of the most popular, and widely used programming languages in the world.
Q: Is Python code compiled or interpreted?
Ans: To shortly answer, it is both compiled and interpreted. However, the compilation step is automatically done, so when we execute the program, we don’t feel it.
Q: Who developed Python programming language?
Ans: Python programming language was developed by Guido Vann Rossum.