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.
import os def PrintHello(str): print ("Hello World "+str) PrintHello("Poftut.com")
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.
$ python2 python_script.py $ python3 python_script.py $ python python_script.py
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.
#!/usr/bin/python import os def PrintHello(str): print ("Hello World "+str) PrintHello("Poftut.com")
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.
$ chmod u+x python_script.py $ ./python_script.py
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
python commands according to our Python version selection. In this example, we will use
$ python3 $ python2
We paste the python script we want to run it will immediately process by the interactive shell-like below.
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.
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.