7

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 ?

7
  • Please post the output of ls -l.
    – manatwork
    Oct 8, 2013 at 11:23
  • here it is, total 16 -rw-r--r-- 1 pi pi 14536 Oct 8 10:44 pyAES.py Oct 8, 2013 at 11:24
  • 1
    So your file is not executable. Run chmod +x pyAES.py then retry.
    – manatwork
    Oct 8, 2013 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 Oct 8, 2013 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. Oct 8, 2013 at 11:52

6 Answers 6

5

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.

2
  • Thank you very much, it worked ! btw, how to directly move a file from /usr/bin to /Desktop ? Oct 8, 2013 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, 2013 at 12:22
3

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

3
  • so, am I supposed to define the complete path of the pyAES.py file ? Oct 8, 2013 at 11:33
  • 1
    yes.. or you can copy or crate symlink in /usr/bin/ Oct 8, 2013 at 11:40
  • how am i supposed to define the complete path when I am already in the directory ? Oct 8, 2013 at 11:42
3

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).

2

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").

0

I faced the same error when I tried py file1.py

The solution is very simple, just type python3 file1.py

-1

I was getting error for writing:

python script.py

solution: change python to python3

python3 script.py

(this may work for ubuntu users)

2
  • Your answer doesn't address the question. The original question asks about a Python script launched from the command line without explicitly typing the name of the interpreter. Also, the error you get from your first Python invocation is not command not found, but an error relative to the Python version required to run the script. Feb 13, 2021 at 6:24
  • In the future, please read the question carefully and only write an answer that corresponds to the question. Also remember that formatting computer code differently from normal text improves the readability. Feb 13, 2021 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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