Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ?

share|improve this question
    
Please post the output of ls -l. –  manatwork Oct 8 '13 at 11:23
    
here it is, total 16 -rw-r--r-- 1 pi pi 14536 Oct 8 10:44 pyAES.py –  Sufiyan Ghori Oct 8 '13 at 11:24
1  
So your file is not executable. Run chmod +x pyAES.py then retry. –  manatwork Oct 8 '13 at 11:25
1  
Most probably the file is not chmoded. python pyAES.py or chmod +x pyAES.py && ./pyAES.py should do the job –  val0x00ff Oct 8 '13 at 11:25
1  
@Xufyan You first need to locate python2 by typing type -p python2 See where the binary is located and put it in the files' header. –  val0x00ff Oct 8 '13 at 11:52
show 2 more comments

4 Answers 4

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you very much, it worked ! btw, how to directly move a file from /usr/bin to /Desktop ? –  Sufiyan Ghori Oct 8 '13 at 12:00
1  
@Xufyan If you mean move as in mv, you'll need to do it as root. But the resulting file on your Desktop will be owned by root, so you'll have to change its ownership. –  Joseph R. Oct 8 '13 at 12:22
    
Thank you Sir, I just generated the symbolic link and it is working fine :) –  Sufiyan Ghori Oct 8 '13 at 12:33
add comment

The first hurdle is that you need to tell the shell where to find the program. If you don't put any directory indication, you can only run executable files located in the executable search path described by the PATH environment variables. The current directory is not in the search path unless you put it there. So run ./pyAES.py.

In order to run a program, you must have execution permissions on it:

chmod +x pyAES.py

If you get a message like “bad interpreter: No such file or directory” or simply “No such file or directory” on a file that exists, it means that there is an error in the script's shebang line. (See /bin/sh: ./check-dependencies.pl: not found — but check-dependencies.pl exists! for a more detailed explanation.) The shebang line is the first line of the script and indicates the location of the interpreter.

To avoid hard-coding the path to an interpreter (e.g. /usr/bin/python or /usr/local/bin/python), you can use the /usr/bin/env program as a trampoline:

#!/usr/bin/env python

PEP 304 specifies that #!/usr/bin/env python2 is the right away to refer to Python 2.x. However there are plenty of existing systems where Python 2.x is only provided under the name python and not python2. So you may have to juggle between the two. If you have root permissions and your distribution only provides Python 2.x as python or only provides python2, create a symbolic link to the other name.

If you see the error “: No such file or directory” (with nothing before the colon), it means that your shebang line has a carriage return at the end, presumably because it was edited under Windows. Remove the CR: the shebang line needs to have a Unix line ending (linefeed only).

share|improve this answer
add comment

The Error "command not found" you are getting because that executable does not exists in /bin/ or /usr/bin/ all paths in $PATH variable.

When you run any command, in backend shell searches that executable/binary in PATH , eg. /bin/ /usr/bin/ etc...

So if path not properly define then you will face this issue.

and when you use " ./command " then it will execute that command from current direcotry , the PATH variable is not used to search for the file name

share|improve this answer
    
so, am I supposed to define the complete path of the pyAES.py file ? –  Sufiyan Ghori Oct 8 '13 at 11:33
1  
yes.. or you can copy or crate symlink in /usr/bin/ –  Rahul Patil Oct 8 '13 at 11:40
    
how am i supposed to define the complete path when I am already in the directory ? –  Sufiyan Ghori Oct 8 '13 at 11:42
add comment

The first line in your python file should like this:

#!/usr/bin/python 

That line shows linux which interpreter to use. If you don't know the path to python, type:

which python

and add the path to the first line (example "#!/PATH"). The file has to executable, you did it with "chmod +x NAME.py". And then add the file (just copy or make a symlink) in a folder that is listed in your PATH env variable (example "/usr/bin" or "/usr/local/bin").

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.