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 ?
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.
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
.
You can install python with:
apt install python
After that, the python
command will work.
$ which python
/usr/bin/python
python -V
Python 2.7.5
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
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:
From the ^M you can see that the file myscript.py is using windows/dos-style line breaks not Linux style
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With