173

I update the kernel, after that the Ubuntu doesn't work well, PS: I try to exec "meld" command, it will report that "/usr/bin/env: python: No such file or directory", then I exec "sudo apt-get install python" and get the result "python is already the newest version.", what should I do for it.


I'm not good at linux, can you tell me how to revert my linux to the last right status, or reinstall the python normally.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Hao
  • 1,801
  • 2
  • 13
  • 8

11 Answers11

325

Problem scenario:

/usr/bin/env: ‘python’: No such file or directory

Possible Solution #1

  • If Python 3 is not installed, install it: apt-get install python3

Possible Solution #2

  • If Python 3 has been installed, run these commands: whereis python3

  • Then we create a symlink to it: sudo ln -s /usr/bin/python3 /usr/bin/python

EDIT: hi everyone, I noticed that @mchid posted a better solution below my answer: sudo apt install python-is-python3.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • 1
    Worked for me as well with Ubuntu 20.04 except that I put the symbolic link into my `/home/user/bin` folder instead as I did not want to clutter up `/usr/bin`. – Richard Chambers Sep 26 '20 at 19:42
  • This obviously fixes this problem, but I'm a little shocked it's the "correct" fix for this. Why isn't the regular python3 on the system path being used? Is this the fault of the developer of the script/application? An old Python 2 standard that fell out of use? Is there some other reason this doesn't "just work"? – Manius Jun 20 '21 at 20:40
  • @Manius, once I tried to remove Python2, I broke my distro so badly that even my Ethernet card wasn't working. I had to manually port Python2 back to my distro with a USB sick and compile it from there. There are tons of legacy code that still need Python2 and believe me, we will not remove Python2 for a long time. For the same reason Windows still has to support all the old C++ libraries and version. It's called Backward compatibility and that's the price to pay. By default the distro needs Python2. – Francesco Mantovani Jun 20 '21 at 21:11
  • @FrancescoMantovani, because half your system is build with python. I remeber when I, as a beginner, run sudo apt uninstall python, and it uninstalled the whole gome desktop with it. I was left with a shell. I also think this isnt a clean solution. Some tools may rely in python2, pretending python3 is python2 could cause undefined behavior. – The Fool Sep 15 '21 at 10:44
  • I faced the sane problem @TheFool but applying the solution I posted hasn't put 131 person into trebles still – Francesco Mantovani Sep 15 '21 at 11:36
  • 9
    I have used the solution below, it feels more safe if it was shipped in an official apt repo `sudo apt install python-is-python3`. – The Fool Sep 15 '21 at 11:38
  • 1
    @TheFool, I upvoted the solution as it was listed below. Nice catch, much better than my hacky way – Francesco Mantovani Sep 15 '21 at 12:53
  • This is great, but how do I undo this? – Sнаđошƒаӽ May 11 '22 at 14:43
  • This way @Sнаđошƒаӽ: https://linuxhint.com/remove-symbolic-link-linux/ . Does the symlink created you a problem? Why do you want to remove it? – Francesco Mantovani May 11 '22 at 18:51
  • 1
    @FrancescoMantovani thanks, being new to Linux it took me a while to realize that it is just a symlink (a close relative of shortcut in Windows), which I can delete as and when I want. So, no it didn't cause me any problem, but just wanted to undo. – Sнаđошƒаӽ May 12 '22 at 14:43
  • creating a symlink hleped. I tried alias but it was useless – N. Raj Nov 09 '22 at 00:21
177

On Ubuntu 20.04 and newer, there is a package to fix this problem. Run the following commands:

sudo apt update
sudo apt install python-is-python3

Run apt-cache show python-is-python3 for more info.

mchid
  • 2,699
  • 1
  • 14
  • 12
  • 1
    I haven't tested this yet, but assuming this works, it kinda seems like this is the "correct" or best answer here (SO vote count fails again) and that the problem is perhaps a result of a changing standard from python2 to 3. Is that accurate? – Manius Jun 20 '21 at 20:42
  • 1
    @Manius They answered in May and I didn't answer until almost December. I mean, this pretty much does the same thing (creates a symlink) so "best" is kind of subjective. But yes, this seems to be a result of changing from python2 to python3 — there's actually a package named `python-is-python2` that creates a symlink to python2 for people who need to run old python software. – mchid Jun 20 '21 at 22:55
  • I get the timing difference yes. I thought subjectivity wasn't allowed on SO? ;) Just picking on SO's arbitrary rule set. We could make an argument that manually creating a link is a distro-neutral solution, I suppose. Python has a way of agitating me to be extra critical for some reason. Python's environment seems to be a mess whenever I need to deal with it. – Manius Jun 23 '21 at 20:15
71

Having been momentarily stumped by this error myself, I thought I'd post how I fixed my problem.

My problem was an error:

: No such file or directory

Which made little sense to me. My problem is that my editor had silently converted the script from Unix LF to Windows CR/LF line-termination. A rather unfortunate upshot of this is that "#!/usr/bin/env python" actually became "#!/usr/bin/env python\015" where \015 is the invisible CR character... /usr/bin/env was, then, unable to find a command "python\015" - hence the file-not-found error.

Converting the script to Unix line-ending convention solved my problem... but only after a few minutes' head-scratching.

aSteve
  • 1,956
  • 1
  • 20
  • 35
  • 6
    thank you very much... and as a side note you can convert win endings into unix endings by `fromdos ` and `fromdos` utility is in `tofrodos` package in standard repos. – destan Feb 26 '13 at 09:36
  • ubuntu should be smart enough to understand both styles. what year is this again? – endolith May 20 '14 at 02:52
  • You can use this in Vim editor to convert file format - :set ff=unix More details in this answer: https://stackoverflow.com/a/82743/9152071 – Tilak M Divakar Jul 18 '20 at 00:18
  • 2
    @endolith it still isn't (well I'm checking on Debian, but I think Ubuntu is the same, the shebang is parsed by Linux the kernel). But the error message is much better now: `/usr/bin/env: ‘python3\r’: No such file or directory` – Ruslan Apr 28 '22 at 14:02
29

May 2022: For anyone who just updated to Monterey 12.3 it appears the update replaces python with python3. Downloading python fixes the issues in Xcode and git command line. Be sure to read the two comments below.

Norman
  • 3,020
  • 22
  • 21
  • 3
    For those that aren't aware: Python 2 has been discontinued, it's not even receiving security updates at this stage. It's really quite important to upgrade to Python 3! – Sebastian Apr 14 '22 at 21:34
  • 8
    I'm using python via homebrew and got a similar error, so inspired by other answers, I ran `sudo ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python` and things magically worked again. Thanks – Sandra May 03 '22 at 09:53
19

For people facing the same issue with MacOS and installed python3 with homebrew:

sudo ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python
DanielM
  • 3,598
  • 5
  • 37
  • 53
  • it's say file exists – Azhar Uddin Sheikh Nov 16 '22 at 09:06
  • When I ran `which python3` it came back with `/usr/bin/python3` So this didn't work. My solution came from [this post](https://stackoverflow.com/questions/71591971/how-can-i-fix-the-zsh-command-not-found-python-error-macos-monterey-12-3) Which came from Apple changing the default of `python` to not have v2.7 – Shadoath Aug 16 '23 at 12:51
12

For those with macOS or M1 machines (tested on 12.5) symlinking /usr/bin/python3 will not work because it's a reference to the xcode python3 installation. Instead do:

sudo ln -s /Library/Developer/CommandLineTools/usr/bin/python3 /usr/local/bin/python

Alex
  • 3,441
  • 4
  • 18
  • 30
9

@mchid's answer is the one you should go for it.

just FYI,

if you do this: $ python

it will say Command 'python' not found ...

But if you do this: $ python3, it should work.

So, just modify the shebang line

from !#/usr/bin/env python to !#/usr/bin/env python3, you're good to go.

(which is automatically done by doing sudo apt install python-is-python3)

starriet
  • 2,565
  • 22
  • 23
5

This answer for android build system error For Python 3

If you get a "/usr/bin/env 'python' no such file or directory" error message, use one of the following solutions: If your Ubuntu 20.04.2 LTS is a newly installed (vs. upgraded) Linux version:

sudo ln -s /usr/bin/python3 /usr/bin/python

f using Git version 2.19 or greater, you can specify --partial-clone when performing repo init. This makes use of Git's partial clone capability to only download Git objects when needed, instead of downloading everything. Because using partial clones means that many operations must communicate with the server, use the following if you're a developer and you're using a network with low latency:

repo init -u https://android.googlesource.com/platform/manifest -b master --partial-clone --clone-filter=blob:limit=10M

you can see document in Downloading the Source

e.moradi
  • 81
  • 1
  • 6
4

creating a symbolic link solved the issue for me

sudo ln -s /usr/bin/python3 /usr/bin/python
3

Additional possible solution if the other suggestions from the mates are not working is to convert the .py scripts into UNIX format.

You can do so by installing dos2unix, before converting your scripts. You can do with something like this:

sudo apt install dos2unix

Once installed, you can convert your script accordingly:

dos2unix <filename>.py

You can read more here about dos2unix.

--

Separately, do try to run your script locally and see if it's working, you will also need to take note to include the hashbang in your script.

#!/usr/bin/env python3
  • Note that above is for python3, use python if necessary.
ahjim0m0
  • 94
  • 2
  • 5
0

follow Alex's instruction, it works like a charm.

https://stackoverflow.com/a/75239468/9610979

sudo ln -s /Library/Developer/CommandLineTools/usr/bin/python3 /usr/local/bin/python
林韋彤
  • 11
  • 1
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34851076) – doneforaiur Aug 22 '23 at 04:35