DEV Community

Ayush Basak
Ayush Basak

Posted on

Is Python compiled or interpreted?

It looks like an easy question to answer, obviously interpreted, because Python code is run line by line at runtime. But it's wrong.

If your are a newbie to programming and have been using a language to code, it might have been obvious to you that some languages are interpreted, some are compiled to machine code, and some are both compiled and interpreted.

For example, when we use the CLI to run

python3 -c "print('Hello World')"

, we see that the code was executed at runtime and hence we assume that all python code is supposed to be interpreted.
Similarly, when we write a C code, we need to compile it to an executable and then run it, and hence we assume that all C code must be compiled.

But the language itself is not either interpreted or compiled. It is not a property of the language. When we see a C code or python code it is nothing but a blot of ink on paper or some random text on a text editor. It is nothing but an idea of concepts in air. Computers cannot understand these text based ideas that humans can, it needs machine code to execute a program.

Hence we need some form of implementation of the language that does the hard work for us. Therefore there exists a Compiler or an Interpreter. It is the compiler's or interpreters job to convert the raw text based code written in any language to machine readable code for execution.

Coming back to the question,
Is python compiled or interpreted?.
Depends. Since most of us use python by the python3 or py command, we are actually using the implementation called CPython written in C. It compiles .py files to .pyc files. .pyc files contain byte codes. The CPython implementation also interprets those byte codes.
There are various other implementations for python such as Jython (written in Java) or PyPy (written in RPython). These are just-in-time compilers for python.

Similarly there exists interpreters for languages such as C.

Conclusion:

It is not the property of a programming language to be either compiled or interpreted but the implementation of the language in use instead.

References:

StackOverflow: Is python interpreted, compiled or both?
StackExchange: Is Python interpreted or compiled?
StackOverflow: CPython is bytecode interpreter?

PS:

Do correct me if I make a mistake in this article. 😊

Top comments (5)

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Hi, here is my input on the topic, from my python background.

Based on this article, Python has both compiled and interpreted parts to it. But the compiled part is minor I think and its still useful to think of Python and interpreted

geeksforgeeks.org/python-compiled-...

Yes Python does compile to .pyc files. They are not used immediately - they are only used when running the application a second time and if the modified date of the .py and .pyc file is the same then it does not recompile the files. So performance is a bit faster on repeat runs. (If you view your .pyc file it will look garbled to you but docstrings actually get preserved as plain text, for interest)

These files are only created when doing module imports (python - m foo or imports within a script) and not when running a script directly (python foo.py). You can also give the python command a flag to disable creating .pyc files.

Yes those byte code .pyc files are compiled files but they still get sent to the python interpreter as machine code. So .pyc files only get us partway there.

Using .pyc files improves performance a bit. But those compiled files will not give you the performance of true compiled languages like C or C++ or Rust.

This is a complex topic. I always thought Java must be compiled because it won't compile if you have bad types, you get compiled files out and you get performance close to C. But the thing is that Java creates byte code when is then interpreted by the JVM (Java Virtual Machine) to machine code and run.

This article covers Java Interpreter Compiler. Note the diagram from high level to byte code to machine code
codespeedy.com/why-java-is-called-...

I don't know about Java anymore - is it both compile and interpreted? Python compiled step is optional and it doesnt have compile time checks so I'd say Python is interpreted. But I don't know how the actual interpreter works to discuss further

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Oh and if you want something like compile time type checks for python, to match C and Java, have a look at Mypy.

From python 3.5 you can use optional type annotations in code and use the mypy command to validate them.

michaelcurrin.github.io/code-cookb...

It is a similar principle to TypeScript adding safety to JS code types. Except here we don't convert
.ts to .js files we just stay on .py files and types get ignored at runtime of your application (ie when MyPy is done running)

Collapse
 
amritanshupandey profile image
Amritanshu Pandey

Very thorough explanation! Thanks!

Collapse
 
ayushbasak profile image
Ayush Basak

Thanks! I learned something new. 😀

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Is Python compiled or interpreted?

Yes