How to run Python scripts from shell

Python is available for many platforms including Linux and Windows. To download it for your system go to https://www.python.org/downloads/

cPanel includes by default Python, so you can easily run scripts – as root or as user.

To run Python scripts from shell:

1. Download the script or create the script file. Python scripts have the .py file extension.

2. Make the script executable with:

# chmod +x python_sript.py

3. Run the script:

# python python_sript.py

Our “Hello Word” Python script python_sript.py content

#! /usr/bin/python
print('Hello World!')

When executed, it will print “Hello World!”

root@web [/temp3]# python python_sript.py
Hello World!
root@web [/temp3]# 

To check where Python is installed on your server:

# whereis python

In most cases Python is installed in /usr/bin/python

root@web [/temp3]# whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/bin/python2.7-config /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
root@web [/temp3]#

If the directory is not added yet to the PATH variable, use:

# export PATH="$PATH:/usr/local/bin/python"

Leave a Reply