Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bad interpreter no such file or directory /usr/bin/python

Tags:

python

I created script python and I moved it to /usr/bin and I named the script by sdfgdgh without .py and I write in script this code

#! /usr/bin/python

print("worked")

and I was given the script chmod +x
but when I type in terminal sdfgdgh give me the error :

bad interpreter no such file or directory /usr/bin/python

why and what is the solution ?

like image 280
vb.net Avatar asked Dec 14 '17 21:12

vb.net


Video Answer


6 Answers

The problem is with your python installation. Probably your /usr/bin/python either does not exist at all or it is a dead symbolic link pointing to non-existing python.

So first solution is to check if /usr/bin/python exists. If so check if it's not dead link and if it is, fix the link to point to existing python intepretter:

cd /usr/bin
sudo ln -fs <full_path_to_existing_python_binary> python

If you can't or don't want to change /usr/bin/python but you have python installed and its location is recognized by the system (i.e. calling python from shell works) you can try changing your script as a workaround:

#! /usr/bin/env python
print("worked")

This way your script will use python as an interpreter regardless of the real python location as long as it is in your PATH.

like image 189
running.t Avatar answered Oct 17 '22 07:10

running.t


I had similar problems, I had installed a package in my Ubuntu 20.04 which depended on Python 2. This messed up the meaning of python. I had to uninstall that package: qjoypad, xboxdrv and one other; uninstall python 2 with sudo apt remove python.

Then to confirm, I used which python which gave a blank output. The next step was to cd /usr/bin and then create a symlink with sudo ln -fs python3 python.

like image 27
Lord_Sarcastic Avatar answered Oct 17 '22 07:10

Lord_Sarcastic


You can install python with:

apt install python

After that, the python command will work.

like image 4
gpresland Avatar answered Oct 17 '22 07:10

gpresland


  1. First check which python you've installed with
$ which python
/usr/bin/python
  1. Then check if it's executable
python -V
Python 2.7.5
  1. If you run a py file with dos format on linux you also will get this issue.

Method a: check dos fileformat with cat -v filepath to see if the line end with ^M.

Method b: vim filepath -> :set ff to check it, for example "eni.py" [dos] 64L, 2151C

Method c: file filepath to check if it has CRLF file_path: Python script, UTF-8 Unicode text executable, with CRLF line terminators

Solution: you can set the filefort to unix with vim filepath then :set ff unix

like image 4
LF00 Avatar answered Oct 17 '22 08:10

LF00


I was encountering the same problem. Maybe you are editing the script on windows as a file and executing it on Linux. below steps resolved the problem for me:

  1. edit on terminal-> sudo vi myscript.py
  2. add python location-> #!/location/anaconda/bin/python

From the ^M you can see that the file myscript.py is using windows/dos-style line breaks not Linux style

like image 2
Sanjib Banerjee Avatar answered Oct 17 '22 06:10

Sanjib Banerjee


A flexible solution would be to point at this address using the address of an arbitrary python version.

Assuming you are using Ubuntu, you can find the installed Python versions with

ls /usr/bin | grep python

For me this printed:

dh_python2
python
python2
python2.7
python3
python3.8
python3.8-config
python3-config
python3-futurize
python3-pasteurize
x86_64-linux-gnu-python3.8-config
x86_64-linux-gnu-python3-config#

Now let's say you want to point to python 3.8. The following line of code presents python3.8 as the 1st alternative(Hence the 1 at the end).

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1

Now whenever the python is referred, instead of the /usr/bin/python folder, /usr/bin/python3.8 will be accessed.

You can enlist other alternatives too. To see which alternatives you have, use

update-alternatives --list python

Finally to switch between those alternatives, use

sudo update-alternatives --config python
like image 2
Kağan Aytekin Avatar answered Oct 17 '22 08:10

Kağan Aytekin