how to run Python scripts on Linux OS

Posted by

Basically, when you are running scripts [python / bash / sh] etc. you will need to check some specs before you run it, at this time the focus is on Python scripts.

#1 Check if the file has the specific “shebang” line as first line of the script.

What is “shebang” ? It’s a simple line that act as variable into the script to indicate the location of the Python interpreter on the system.

For example,

#!/usr/bin/python

Note: Check the syntax, if does not have the correct order it will fail.

#! /path/to/python/interpreter

#2 Check permissions of the script, the script will need execute permissions.

For example,

-rwxr-xr-x. 1 root root 0 Jan 31 00:12 extract_rules.py

You can assign execute permissions to the script with the following command,

[root@localhost ~]# chmod +x filename.extension

Note: Extension, because the command works to assign execute permissions to any type of scripts.

[root@localhost ~]# chmod +x extract_rules.py

#3 Now you only need to run the script, to do that just type the word “python” at the beginning of the script you want to run and wait until the task is done. That will invoke the python interpreter located on the specific path we used as shebang.

[root@localhost ~]# python extract_rules.py

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s