Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's preventing python from being compiled?

Tags:

python

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.

like image 214
NerdOfLinux Avatar asked Aug 29 '17 01:08

NerdOfLinux


People also ask

Could Python be compiled?

For the most part, Python is an interpreted language and not a compiled one, although compilation is a step. Python code, written in . py file is first compiled to what is called bytecode (discussed in detail further) which is stored with a . pyc or .

How Python gets compiled?

The compilation part is done first when we execute our code and this will generate byte code and internally this byte code gets converted by the python virtual machine(p.v.m) according to the underlying platform(machine+operating system).


1 Answers

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.

like image 179
Russell Borogove Avatar answered Oct 09 '22 15:10

Russell Borogove