Execution of a Python Program

To execute a Python program in Windows, follow these steps:

  1. Install Python: Download and install the latest version of Python from the official Python website (https://www.python.org/downloads/). Follow the instructions to complete the installation process.
  2. Create a Python file: Create a new text file and save it with a .py extension. This will be your Python script file.
  3. Write your Python code: Open the Python file in a text editor and write your Python code.
  4. Open the Command Prompt: Press the “Windows key + R” to open the Run dialog box, type “cmd” and press Enter. This will open the Command Prompt.
  5. Navigate to the directory: Use the cd command to navigate to the directory where your Python script file is saved. For example, if your file is saved on the desktop, you can use the following command:
    cd C:\Users\YourUserName\Desktop
  6. Execute the Python file: To execute the Python file, type python followed by the name of your Python script file (including the .py extension). For example:
    python my_script.py

    This will run your Python program and display the output in the Command Prompt.

The Python language is similar to C, Perl and Java. However, there are some differences between the languages.

There are three ways of executing a Python Program.

  1. Using Python’s command line window
  2. Using Python’s IDLE graphics window
  3. Directly from System Prompt

1. Using Python’s Command Line Window

Click the Python’s Command Line icon. This will open Python’s Command line. We see >>> symbol which is called Python Prompt

2. Using Python’s IDLE Graphics Window

We can execute a Python program by Integrated Development Environment (IDLE) which provides graphical interface to the user. Click the Python’s IDLE icon. This will open the Python’s IDLE window.

python-idle-graphic-window

3. Running Directly from the System Prompt

In most programming environments like Java or .NET, we can generally type the program in a Text editor like Notepad or EditPlus. After typing the entire program, we save it and then call the compiler to compile the program.This can be done in Python also. Let’s perform the following steps to run the program directly from the system prompt.

a. Open a Text Editor and type the program, as shown in figure –

Typing Python program in Notepad

b. Save the following program by clicking File->Save As. Type the following name as “first.py”, as – 

Saving the python program in a directory

c. Open the Command Prompt (or DOS prompt) window and go to that directory where the program is saved.This can be done by typing COMMAND or CMD commands at Start->Run.

Going to DOS Prompt

d. Click the ‘OK’ button. This opens the Command Prompt window.

e. Now go to that directory where your program is saved. Suppose my program is saved on Desktop in a folder named “py”. So, the directory using CD in CMD as:

CD  C:\Users\deepak\Desktop\py 

To see the directory content whether our first.py program is already available in that directory or not.

 C:\Users\deepak\Desktop\py > dir

Entering the directory where python program is saved

f. Let’s now execute the first.py program by calling the python command.

C:\Users\deepak\Desktop\py > python first.py

Running the python program at System Prompt

Execution of a Python Program Internally

Suppose a python program is saved with the name x.py. Here, x is the program name and .py is the extension name. Every Python program is typed with an extension name .py.

After typing the program, the next step is to compile the program using Python Compiler. The compiler converts the Python program into another code called Byte Code.

Byte Code represents a fixed set of instructions that represents all operations like Arithmetic operations, Comparison operations, Memory related operations, etc., which run on any operating system and hardware. It means the byte instructions are system independent or platform independent. The size of each byte code instruction is 1 byte and hence they are called with the name byte code. These byte code instructions are contained in the file x.pyc. Here, the x.pyc file represents a python compiled file. The next step is to run the program. If we directly give the byte code to the computer, it cannot execute them. Any computer can execute only binary code which comprises 1s and 0s. Since the binary code is understandable to the machine(computer), it is also called machine code. It is therefore necessary to convert the byte code into machine code so that our computer can understand and execute it. For this purpose, we should use PVM (Python Virtual Machine).

PVM uses an interpreter which understands the byte code and converts it into machine code. PVM first understands the processor and operating system in our computer. Then it converts the byte code into machine code understandable to that processor and into that format understandable to that operating system. These machine code instructions are then executed by the processor and results are displayed.

An interpreter translates the program source code line by line. Hence, it is slow. The interpreter that is found inside the PVM runs the python program slowly. To rectify this problem, in some flavors of Python, a compiler is added to the PVM. This compiler also converts the byte codes into machine code but faster than the interpreter. This compiler is called JIT (Just In Time) compiler. The advantage of JIT compiler is to improve speed of execution of a Python program and thus improving the performance. JIT Compiler is not available in all Python environments. For example, the standard python software is called CPython which is created using C language that does not include a JIT compiler. But PyPy, which is created in Python language itself uses a JIT compiler in addition to an interpreter in the PVM. So, the PyPy flavor of Python definitely offers faster execution of the Python programs than CPython.

Normally, when we compile a Python program, we can not see the .pyc file produced by the Python compiler and the machine code generated by the PVM. This is done internally in the memory and the output is finally visible. For example, if our Python program name is x.py, we can use Python compiler to compile it as: C:\>python x.py

If we observe, we cannot find any files in the directory with the extension .pyc. The reason is that all the steps in the sequence: source code -> byte code -> machine code -> output are internally completed and then the final result is displayed. Hence, after execution, we cannot find any .pyc file in the directory. To separately create .pyc file from the source code, we can use the following command:

C:\>python -m py_compile x.py

Here, we are calling the Python compiler with -m option. -m represents module and the module name is py_compile. This module generates the .pyc file for the specified .py file. The compiler creates a separate directory in the current directory by the name pycache where it stores the .pyc file. The .pyc file name may be something like: x.cpython-34.pyc. The cpython here indicates that we are using the Python compiler that was created using C. This is of course, the standard Python compiler.

So, the question is “What is the use of the .pycfiles? We know that the .pyc files contain byte code. We get these files after the compilation is completed. Hence, the first step is over. The next step is to interpret them using PVM. This can be done by calling the Python compiler as:

C:\>python x.cpython-34.pyc

In this case, we are supplying .pyc file to the Python compiler. Now, Python compiler will skip the first step where it has to convert the source code into byte code as already it sees the byte code inside the .pyc file. This file is executed directly by the PVM to produce the output. Hence, the program takes less time to run and the performance will be improved. This is the reason that after the completion of the project, the .pyc files are distributed to the user who can directly run these files using PVM and view the output.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial