Skip to content
Advertisement

What’s preventing python from being compiled?

I understand that Python is an interpreted language, but the performance would be much higher if it was compiled.

  • What exactly is preventing python from being compiled?
  • Why was python designed as an interpreted language and not a compiled one in the first place?

Note: I know about .pyc files, but those are bytecode, not compiled files.

Advertisement

Answer

Python, the language, like any programming language, is not in itself compiled or interpreted. The standard Python implementation, called CPython, compiles Python source to bytecode automatically and executes that via a virtual machine, which is not what is usually meant by “interpreted”.

There are implementations of Python which compile to native code. For example, the PyPy project uses JIT compilation to get the benefits of CPython’s ease of use combined with native code performance.

Cython is another hybrid approach, generating and compiling C code on the fly from a dialect of Python.

However, because Python is dynamically typed, it’s not generally practical to completely precompile all possible code paths, and it won’t ever be as fast as mainstream statically typed languages, even if JIT-compiled.

Advertisement