Python is a popular programming language used to create applications and scripts. The python script is a file that contains Python code. The Python code consists of different statements. The Python files have the *.py
extension which is the short form of the “python”. Alternatively *pyw
is used for Python script for Windowed mode, *.py3
is for Python3 script, *.pyc
for Python compiled bytecode. A Python script can be executed in different ways.
Example Hello World Script for Python
During this tutorial, we will use the following sample Hello World script. The following script simply prints “Hello World” to the standard output which is generally the terminal or command-line interface.
#!/bin/python
print("Hello World")
Print Python Interpreter Version
Before trying to run a Python script the version of the Python script and Python interpreter is very important. Python has two major versions called Python2 and Python3. Even most of the Python features are compatible for both versions there are some incompatibilities between them which may result in an abort of script execution. The Python interpreter version can be displayed with the --version
option with the python command.
$ python --version
The Python version output is like below which is Python3 major version with 9.5
minor version.
Python 3.9.5
If you know that you are running Python2 but do not know the minor version number the following command can be used to display Python2 version with the major and minor version numbers.
$ python2 --version
The equivalent of the Python3 is like below.
$ python3 --version
Run Python Script via Command Line Interface
Python script can be executed via the command-line interface. The command-line interface or CLI can be the MS-DOS, PowerShell, Bash, etc. It is not important. The most important thing to run Python script via command line is setting the Python interpreter where the python
command or python.exe
for Windows operating systems can be executed. The helloworld.py
script can be executed like below if the script is located in the current working directory.
$ python helloworld.py
If the python script is not located in the current working directory the complete or absolute path can be provided for execution. In the following example, we executed the Python script which is located in the user “ismail” home directory in Linux.
$ python /home/ismail/helloworld.py
If you are using Windows operating system and want to run a Python script that is located under specific user Desktop use the following command.
> python.exe C:\Users\ismail\Desktop\helloworld.py
Run Python Script via Python Interactive Shell
Python uses the modules in order to put different functions, structures to the related space. The import
statement is used to import different built-in and 3rd party modules. The import can be used to import a *.py file which statements exception, class, function etc. inside the *.py file will be executed. First we should open Python Interactive Shell by using the python
command like below.
$ python
In the interactive shell use the import statement with the Python file name.
>>> import helloworld.py