35

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?

10
  • 3
    What is the output of which python and ls /usr/bin/python? Commented Dec 14, 2017 at 21:53
  • Are you sure you have python installed on your system? If so what where do you have it installed ? Commented Dec 14, 2017 at 21:57
  • Seems like the operating system is not recognizing the location of Python. You have not added python to your PATH in your .bashrc file. Commented Dec 14, 2017 at 21:58
  • 2
    @karthikbharadwaj There is no need to add anything in .bashrc since python interpreter is defined here as /usr/bin/python Commented Dec 14, 2017 at 22:00
  • 1
    You should also check the encoding of the source file. If it is UTF-8 with BOM the sequence "#!" will not work. Commented May 14, 2019 at 18:32

9 Answers 9

29

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i find the solution now i copied my script to /bin and now work may be don't work if was in like path of the interpreter , thank you all for help me
sudo ln -fs /s/miniconda2/bin/python python and it worked
Worked on default Mac python installation using #! /usr/bin/env python3. Thank you.
9
  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

1 Comment

There is a nice utility, dos2unix, that you can install which converts the file from dos to unix format.
6

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.

1 Comment

I tried this, but on montery it gives me ln: /usr/bin/python: Operation not permitted
4

You can install python with:

apt install python 

After that, the python command will work.

Comments

4

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 

Comments

2

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

Comments

1

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

Comments

0

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

Comments

-1

Simply run this :-

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

2 Comments

A direct solution is welcome, but please ensure you add context around it so your fellow users will understand how it solves the problem
This gives me /usr/lib/cnf-update-db: not found

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.