Executing the python Script
When we speak of Python we often mean not just the language but also the implementation. Python is actually a specification for a language that can be implemented in many different ways.
Background
Before proceeding further let us understand the difference between bytecode and machine code(native code), compiler and interpreter.
Machine Code(aka native code)
Machine code is set of instructions that directly gets executed by the CPU. Each instruction performs a very unique task, such as load or an logical operation on data in CPU memory. Almost all the high level languages such as C translate the source code into executable machine code with the help of Compilers, loaders and linkers. Every processor or processor family has its own machine code instruction set.
Bytecode
Bytecode is also binary representation executed by virtual machine (not by CPU directly). The virtual machine (which is written different for different machines) converts binary instruction into a specific machine instruction. One of the language that uses the concept of Bytecode is Java.
Machine Code is much faster as compared to Bytecode but Bytecode is portable and secure as compared to Machine Code.
Compiler A compiler is a computer program that transforms (translates) source code of a programming language (the source language) into another computer language (the target language). In most cases compilers are used to transform source code into executable program, i.e. they translate code from high-level programming languages into low (or lower) level languages, mostly assembly or machine code.
Interpreter The interpreter is an alternative for implementing a programming language and does the same work as a compiler. Interpreter performs lexing, parsing and type checking similar to a compiler. But interpreter processes syntax tree directly to access expressions and execute statement rather than generating code from the syntax tree.
Implementations of Python
Cpython: The default implementation of the Python programming language is Cpython. As the name suggests Cpython is written in C language. Cpython compiles the python source code into intermediate bytecode, which is executed by the Cpython virtual machine. CPython is distributed with a large standard library written in a mixture of C and Python. CPython provides the highest level of compatibility with Python packages and C extension modules. All versions of the Python language are implemented in C because CPython is the reference implementation.
Some of the implementations which are based on CPython runtime core but with extended behavior or features in some aspects are Stackless Python, wpython, MicroPython.
Stackless Python — CPython with an emphasis on concurrency using tasklets and channels (used by dspython for the Nintendo DS)
Other Implementations
There are some other implementations of the Python language too The only implementations that are known to be compatible with a given version of the language are IronPython, Jython and PyPy.
Jython
Jython is an implementation of the Python programming language that can run on the Java platform. Jython programs use Java classes instead of Python modules .Jython compiles into Java byte code, which can then be run by Java virtual machine. Jython enables the use of Java class library functions from the Python program. Jython is slow as compared to Cpython and lacks compatibility with CPython libraries.
Python uses the interpreter as well as the compiler. How?
In Python, the source is compiled into a much simpler form called bytecode. These are instructions similar in spirit to CPU instructions, but instead of being executed by the CPU, they are executed by a software called a virtual machine. (These are not VM’s that emulate entire operating systems, just a simplified CPU execution environment.) .This is a similar approach to the one taken by Java. There is even a way of translating Python programs into Java byte code for the Java Virtual Machine (JVM). This can be achieved with Jython.
but in python we don’t have to compile program manually it is done by python for us automatically.
Here’s an example of a short Python function, and its bytecode:
The dis module in the Python standard library is the disassembler that can show you Python bytecode. It’s also the best (but not great) documentation for the bytecode itself.
another way to compile this is as follow:
it generates compiled file named python_source_file.cpython-36.pyc inside the __pycache__ folder in the current directory.
You can also automatically compile all Python files using the compileall module.
The compilation is hidden from the user for a good reason. Some newbies to Python wonder sometimes where these ominous files with the .pyc suffix might come from. If Python has write-access for the directory where the Python program resides, it will store the compiled byte code in a file that ends with a .pyc suffix. If Python has no write access, the program will work anyway. The byte code will be produced but discarded when the program exits.
Whenever a Python program is called, Python will check, if a compiled version with the .pyc suffix exists. This file has to be newer than the file with the .py suffix. If such a file exists, Python will load the byte code, which will speed up the start-up time of the script. If there exists no byte code version, Python will create the byte code before it starts the execution of the program. Execution of a Python program means execution of the byte code on the Python Virtual Machine (PVM).
An important aspect of Python’s compilation to bytecode is that it’s entirely implicit. You never invoke a compiler, you simply run a .py file. The Python implementation compiles the files as needed. This is different than Java, for example, where you have to run the Java compiler to turn Java source into compiled class files. For this reason, Java is often called a compiled language, while Python is called an interpreted language. But both compile to bytecode, and then both execute the bytecode with a software implementation of a virtual machine.
Another important Python feature is its interactive prompt. You can type Python statements and have them immediately executed. This interactivity is usually missing in “compiled” languages, but even at the Python interactive prompt, your Python is compiled to bytecode, and then the bytecode is executed. This immediate execution and Python’s lack of an explicit compile step are why people call the Python executable “the Python interpreter.”
This shows just how flimsy the words “interpreted” and “compiled” can be. Like most adjectives applied to programming languages, they are thrown around as if they were black-and-white distinctions, but the reality is much subtler and complex.
Finally, how your program gets executed isn’t a characteristic of the language at all: it’s about language implementation. I’ve been talking here about Python, but this has really been a description of CPython, the usual implementation of Python, so-named because it is written in C. PyPy is another implementation, using a JIT compiler to run code much faster than CPython can.
But why Python needs both a compiler and an interpreter?
Speed. Strict interpretation is slow. Virtually every “interpreted” language actually compiles the source code into some sort of internal representation so that it doesn’t have to repeatedly parse the code. In python’s case it saves this internal representation to disk so that it can skip the parsing/compiling process next time it needs the code.
Is .pyc file (compiled bytecode) is platform independent?
Compiled Python bytecode files are architecture-independent, but VM-dependent. A .pyc file will only work on a specific set of Python versions determined by the magic number stored in the file.
Conclusion
is Python compiled? Yes. Is Python interpreted? Yes. Sorry, the world is complicated… Python as a programming language has no saying about if it’s an compiled or interpreted programming language, only the implementation of it. The terms interpreted or compiled is not a property of the language but a property of the implementation. Python program runs directly from the source code . so, Python will fall under byte code interpreted. The .py source code is first compiled to byte code as .pyc. This byte code can be interpreted (official CPython), or JIT compiled (PyPy).