vb.net
vb.net

Reputation: 459

bad interpreter no such file or directory /usr/bin/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 ?

Upvotes: 33

Views: 199156

Answers (9)

phatmann
phatmann

Reputation: 18513

I got this error when I ran virtualenv ven3 twice by mistake, as it corrupted the ven3 directory.

Upvotes: 0

Edem Robin
Edem Robin

Reputation: 41

I know my answer is late but, I believe it can be of help to someone.

First: Check which python version you are using. which python3 Second: This will print the exact path for you. /usr/bin/python3 Third: Copy the path and use in your script. #! /usr/bin/python3

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28564

  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

Upvotes: 9

Kağan Aytekin
Kağan Aytekin

Reputation: 63

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

Upvotes: 4

running.t
running.t

Reputation: 5709

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.

Upvotes: 29

Sanjib  Banerjee
Sanjib Banerjee

Reputation: 21

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

Upvotes: 2

lordsarcastic
lordsarcastic

Reputation: 369

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.

Upvotes: 6

gpresland
gpresland

Reputation: 1819

You can install python with:

apt install python

After that, the python command will work.

Upvotes: 4

Praveen Kalarikkal
Praveen Kalarikkal

Reputation: 17

Simply run this :-

sudo bash -c "test -e /usr/bin/python || (apt -qqy update && apt install -qy python-minimal)

Upvotes: -1

Related Questions