Running python script from Linux Terminal
Solution 1
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.
Solution 2
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
Solution 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).
Solution 4
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").
Related videos on Youtube
Dark Coder
Updated on September 18, 2022Comments
-
Dark Coder over 1 year
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 ?
-
manatwork over 10 yearsPlease post the output of
ls -l
. -
Dark Coder over 10 yearshere it is, total 16 -rw-r--r-- 1 pi pi 14536 Oct 8 10:44 pyAES.py
-
manatwork over 10 yearsSo your file is not executable. Run
chmod +x pyAES.py
then retry. -
Marek Zakrzewski over 10 yearsMost probably the file is not chmoded.
python pyAES.py
orchmod +x pyAES.py && ./pyAES.py
should do the job -
Dark Coder over 10 yearswhen i typed chmod +x pyAES.py, nothing is showing as output, please read my above post i have added the details.
-
Marek Zakrzewski over 10 years@Xufyan You first need to locate
python2
by typingtype -p python2
See where the binary is located and put it in the files' header. -
Dark Coder over 10 yearsThank you very much, it worked ! btw, how to directly move a file from /usr/bin to /Desktop ?
-
-
Dark Coder over 10 yearsso, am I supposed to define the complete path of the pyAES.py file ?
-
Rahul Patil over 10 yearsyes.. or you can copy or crate symlink in
/usr/bin/
-
Dark Coder over 10 yearshow am i supposed to define the complete path when I am already in the directory ?
-
Dark Coder over 10 yearsThank you very much, it worked ! btw, how to directly move a file from /usr/bin to /Desktop ?
-
Jeff Hewitt over 10 years@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. -
berndbausch about 3 yearsYour 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. -
berndbausch about 3 yearsIn 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.