How To Run/Execute Python Script Tutorial with Examples? – POFTUT

How To Run/Execute Python Script Tutorial with Examples?

Python is popular programming and scripting language. This popularity causes different use cases for Python scripts and codes. In this tutorial, we will learn what is a Python script, how to run Python code and script from the command line, GUI, IDe, etc.

What Is Python Script?

The python script is some code written in Python which can be run as an application or from a shell easily. Python scripts have some structures like below.

  • Import required modules
  • Define Class, Function and other structures
  • Execute and run code and script from imported modules, defined classes, and functions

Example Python Script

Below we can find an example script which will simply print some text by using functions to the standard output.

What Is Python Script?

What Is Python Script?

We can use the command-line interface or the operating system terminal in order to run and execute Python scripts and code. There is a two-way method to run the Python script. First is using the python interpreter and provide the script file path where the interpreter will read the script file and run it. Second is making the script file executable and running from the command directly providing the script file name like a command or executable.

Run with Python Interpreter

We can use Python interpreter which can be run directly from the command line. For Python2 python2 for Python3 python3interpreter can be used. also the interpreter should be added to the OS Path or environment variables which are described below for Windows operating systems. Linux adds path information by default.

LEARN MORE  Introduction To Bash Scripting

Run with Python Interpreter

Run with Python Interpreter

Run Making Python Script File Executable

For Linux Python script files can be made executable. In order to make a script directly executable from the shell, we have added a line which specifies the script type as Python and the python interpreter.

The script file the latest content will be like below.

Run Making Python Script File Executable

Run Making Python Script File Executable

Then we will make the script file named python_script.py user executable with the following chmod command. And now we can run the script file directly from the command line by specifying its name.

Run Making Executable

Run Making Executable

Run Python Script From Python Interactive Mode or Python Shell

Python interpreter is also provided as an interactive shell. Python interactive shell provides the same environment for the Python scripts and programs. We can run a script from the Python shell. directly typing it or copy-paste. First, we will open a Python interactive shell with one of python2, python3 or python commands according to our Python version selection. In this example, we will use python2 shell.

Run Python Script From Python Interactive Mode or Python Shell

Run Python Script From Python Interactive Mode or Python Shell

We paste the python script we want to run it will immediately process by the interactive shell-like below.

Run Python Script From Python Interactive Mode or Python Shell

Run Python Script From Python Interactive Mode or Python Shell

Run Python Script From IDE Like

While deloping simple or complex Python scripts or applications IDE’s can be used. Eclipse, PyCharm, Atom, VSCode and other IDE’s provide directly run the script from IDE and show the output inside the IDE output pane.

LEARN MORE  Python String Variable Type

Run Python Script By Double Click From File Manager

Another way is using the operating system file explorer and double click to the script file. This can be done operating systems like Linux, Windows, MacOSX but the script file should be executable which is described in Run Making Python Script File Executable .Then double click to the python script file.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)