Saturday, July 4, 2015

Python Basics-3


Since Python contains a Interpreter, is that a Interpreted language?
Python falls under byte code interpreted. As we know the extension of a Python language is “.py”. The source code of this file is first compiled to byte code with extension “.pyc”. Python byte code is not binary machine code (e.g., instructions for an Intel chip). Byte code is a Python-specific representation.

The interpretations of Byte code differs with different implementation of Python.  This byte code can be interpreted (official CPython), or JIT compiled (PyPy). Python source code (.py) can be compiled to different byte code also like IronPython (.Net) or Jython (JVM).

Tip – In order to Find the Python Implementation we can use

>>> import platform
>>> platform.python_implementation()
'CPython'

The Current Implementation of Python is CPython. There is different implementations of the Python language and byte code interpreted one is the default one.

The “.pyc” files will be generated when python has a Write-access for the directory where the python program resides. The generated byte code with the extension “.pyc” will be stored in the same location as source code. If Python does not have write-acess ,the byte code will be generated in memory and will discarded upon the execution of the code is completed.

So once the Python byte code is created , it is then moved for execution to some thing generally known as the Python Virtual machine also called as PVM.

The PVM is the runtime engine of Python; it’s always present as part of the Python system, and is the component that truly runs your scripts. 

Do I need to compile Python programs before running them?
No, we don’t need to compile the Python programs as they will be compiled automatically done. But we can compile the Python code by using

[djas999@vx181d imp]$ python
Python 2.7.6 (default, May 12 2014, 15:22:29)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import py_compile
>>> py_compile.compile("hai.py")

Using the py_compile module, we can  compile the code and when we check the list of files we see

[aprk213@vx181d imp]$ ls –alrt
-rw-r--r--  1 djas999 jasgrp   38 Nov  6 03:44 hai.py
-rw-r--r--  1 djas999 jasgrp  106 Nov  6 03:45 hai.pyc

We can compile the code in the script mode as
python -m py_compile my_first_simple_script.py

First, there will be a new subdirectory "__pycache__", if it hasn't already existed. You will find a file "my_first_simple_script.cpython-34.pyc" in this subdirectory. This is the compiled version of our file in byte code

Now if you want to compile all python files in a sub-directory we can use the compileall module as

[djas999@vx181d imp]$ python -m compileall .
Listing . ...
Compiling ./cleanLogs.py ...
Compiling ./cleanLogs1.py ...
Compiling ./cleanlogs.py ...
Compiling ./kai.py ...
Compiling ./test.py ...

How does Python Deal with the Compiled code?
When ever a Python program is called , the python will check if there exists a compiled version of the code with the “.pyc” extension. It also make sure that the “.pyc” extension file is newer than the Source code file. if the file exists , python will load the byte code thus speeding up the execution with out again compiling into byte code.

If there is no Compiled code file newer than the source code file, the code is compiled and then executed

Python Extensions and meanings
As said python extension for source code is “.py” and compiled code is “.pyc”. There are some other extensions in Python which is

.py - Regular scripts
.py3 - Python3 script.
.pyc - compiled script (Bytecode)
.pyo - optimized pyc file
.pyw - Python script for Windows. It is executed with pythonw.exe
.pyx - Cython src to be converted to C/C++
.pyd - Python script made as a Windows DLL
.pxd - Cython script which is equivalent to a C/C++ header

.py[cod] - wildcard notation ,that means the file may be ".pyc", ".pyo", or ".pyd".

No comments :

Post a Comment