Let's create our first Python program. Create a file and name it
hello.py and in any editor let's write a line of code:
print('Hello World') # Our first line of code
The above program when run, prints out "Hello World!" onto the screen, and the stuff after the
# is considered as a comment. Comments and the various types will be covered in a later post, but for now let's assume that it is a line which will have no effect upon execution of the program.
Every program written in Python should have an extension
.py in order to be called a Python program.
The next step after writing the code, is compiling the program into the byte code. This is done by the Python compiler. The byte code instructions are contained in a file
hello.pyc, where the
.pyc extension suggests a python compiled file.
Any computer can execute only binary code (consisting of 1s and 0s), and hence it is essential to convert the byte code into machine code (binary code) so that the computer is able to execute it. This conversion is done by the PVM (Python Virtual Machine). PVM uses an interpreter which understands the byte code and converts it into machine code understandable to the operating system.
An interpreter translates the program source code line-by-line and hence is slower to execute the program. The PyPy flavor of Python utilizes a JIT (Just In Time) compiler in the PVM instead of an interpreter so as to run the programs faster.
Normally, while executing a Python program, the
.pyc files produced are not visible and the necessary steps / working is done internally by the Python compiler and the PVM in order to display the output on the screen.
In order to execute the file, we use the
python command to call the Python compiler as follows:
>> python hello.py
While taking a closer look at the directory, we can see that no file with a
.pyc extension exists. All of the steps have been completed internally and only the final output is displayed.
To separately create a
.pyc file from the source code, we can use the following command:
>> python -m py_compile hello.py
-m option represents module and the
py_compile module generates the
.pyc file for the specified
.py file. The compiler creates a separate directory __pycache__ where the
.pyc file is stored. This file contains the machine code instructions and can be run on any platform using the PVM.
- CPython - The standard Python compiler implemented in the C language.
- Jython - Implementation of Python designed to run on Java platform (executed by JVM).
- IronPython - Implementation of Python for .NET framework, written in C# (C Sharp) language.
- PyPy - Implementation of Python using Python, written in RPython (created in Python language).
- RubyPython - Designed for applications that utilize the Ruby language.
When a developer creates a software in Python, the 2 possible ways to produce the software to the end user include:
- Providing the
- Providing frozen binaries (
.exefiles) The first method expects the user to install PVM and run the
.pycfiles. While, the second method provides the
.pycfiles along with the PVM and the necessary Python libraries. These separate files are converted into a single executable file (generally with a
.exeextension), so that the user can directly execute the file by double clicking it.
Frozen binaries have a slightly larger size as compared to the
.pyc files, since they contain the PVM as well as the library files. For creating these frozen binaries, we require third-party softwares. For example, py2exe is a software the produces frozen binaries for the Windows operating system, and pyinstaller is a similar tool for UNIX or LINUX.