DEV Community

Cover image for The bare-bones of a Python program

The bare-bones of a Python program

mcscodes profile image Mcvean Soans Updated on ・3 min read

Let's create our first Python program. Create a file and name it and in any editor let's write a line of code:

print('Hello World') # Our first line of code
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The -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.

Some Flavors of Python (Python compilers)

  • 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.

Frozen Binaries (Executables)

When a developer creates a software in Python, the 2 possible ways to produce the software to the end user include:

  1. Providing the .pyc files
  2. Providing frozen binaries (.exe files) The first method expects the user to install PVM and run the .pyc files. While, the second method provides the .pyc files along with the PVM and the necessary Python libraries. These separate files are converted into a single executable file (generally with a .exe extension), 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.

Discussion (0)

Editor guide