Python – Running python script from Linux Terminal

executablepathpythonshebang

I have downloaded this script named, pyAES.py and put it in a folder name codes, inside a Desktop directory of my Linux,

According to this example,
http://brandon.sternefamily.net/2007/06/aes-tutorial-python-implementation/

When I type,

./pyAES.py -e testfile.txt -o testfile_encrypted.txt

the file pyAES.py should be executed.
but I am getting this error,

pi@raspberrypi ~/Desktop/Codes $ pyAES.py
-bash: pyAES.py: command not found

the output of ls -l command is,

pi@raspberrypi ~/Desktop/Codes $ ls -l
total 16
-rw-r--r-- 1 pi pi 14536 Oct  8 10:44 pyAES.py

Here is the output after chmod +x

pi@raspberrypi ~/Desktop/Codes $ chmod +x pyAES.py                              pi@raspberrypi ~/Desktop/Codes $
pi@raspberrypi ~/Desktop/Codes $ pyAES.py
-bash: pyAES.py: command not found
pi@raspberrypi ~/Desktop/Codes $

and the command, chmod +x pyAES.py && ./pyAES.py gives the following error,

-bash: ./pyAES.py: /usr/bin/python2: bad interpreter: No such file or directory

I have also tried moving the file in /usr/bin directory and then executing it,

pi@raspberrypi /usr/bin $ pyAES.py
-bash: /usr/bin/pyAES.py: /usr/bin/python2: bad interpreter: No such file or directory
pi@raspberrypi /usr/bin $

I can see the file is present in /usr/bin directory but it is still giving an error that No such file or directory.

I want to know why the Linux terminal is not executing the python script ?

Best Answer

It seems you have a badly-written shebang line. From the error you're getting:

-bash: /usr/bin/pyAES.py: /usr/bin/python2: bad interpreter: No such file or directory

I'd say you should set the first line of /usr/bin/pyAES.py to

#!/correct/path/to/python

where the /correct/path/to/python can be found from the output of:

type -P python

It's /usr/bin/python (not /usr/bin/python2) on my system.

Related Question