Introduction to Python – Executing Python Script

In this Page, We are Providing Introduction to Python – Executing Python Script. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Introduction to Python – Executing Python Script

Executing Python script

As discussed in the previous section, the Python script can be executed using the F5 functional key, from Python’s IDE. It can also be executed using a command prompt by typing the following command:

$ python <filename>

On different platforms, the execution of Python scripts (apart from running from inside Python’s IDE) can be carried out as follows:

Linux

On Unix/Linux system, Python script can be made directly executable, like shell scripts, by including the following expression as the first line of the script (assuming that the interpreter is on the user’s PATH) and giving the file an executable mode.

#! /usr/bin/env python

The ‘# !’ must be the first two characters of the file. Note that the hash or pound character ‘#’ is used to start a comment in Python. The script can be given an executable mode/permission, using the chmod command:

$ chmod + x HelloWorld.py

Windows

On the Windows system, the Python installer automatically associates .py files with python.exe, so that double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed. At MS-DOS prompt, the Python script can be executed by going to the directory containing the script and just entering the script name (with extension).