194

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.

4
  • 1
    What are you trying to do? Access python from /usr/bin/env?
    – gary
    Commented Sep 7, 2010 at 2:19
  • Looks like you have the wrong PATH set in the environment at that point. Use ubuntu.stackexchange.com for ubuntu-specific questions! Commented Sep 7, 2010 at 2:25
  • or go here ubuntuforums.org
    – gary
    Commented Sep 7, 2010 at 2:51
  • Possible duplicate of stackoverflow.com/questions/34109771/…
    – tripleee
    Commented Aug 7, 2020 at 14:52

11 Answers 11

354

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.

11
  • 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. Commented Sep 26, 2020 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
    Commented Jun 20, 2021 at 20:40
  • 11
    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
    Commented Sep 15, 2021 at 11:38
  • 1
    @TheFool, I upvoted the solution as it was listed below. Nice catch, much better than my hacky way Commented Sep 15, 2021 at 12:53
  • 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. Commented May 12, 2022 at 14:43
205

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.

7
  • 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
    Commented Jun 20, 2021 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
    Commented Jun 20, 2021 at 22:55
  • 1
    @EugenKonkov As stated, you can run apt-cache show python-is-python3 for an extremely detailed description. I don't think the description provided could be more to the point and concise, while fully conveying exactly what the package is and does. TLDR: it's a convenience package that ships a symlink to point the /usr/bin/python interpreter to the current default python3.
    – mchid
    Commented Oct 5, 2023 at 9:16
  • 1
    @EugenKonkov If you're not running a Debian/Ubuntu based system and you would like to replicate what this package does, then see this answer to manually create a symlink to python3.
    – mchid
    Commented Oct 5, 2023 at 9:24
  • 1
    @EugenKonkov Alternatively, you can edit the shebang on the first line of your python script to point directly to python3. So, instead of #!/usr/bin/python or #!/usr/bin/env python, you can edit the python script to use #!/usr/bin/python3 or #!/usr/bin/env python3 instead. But you would need to do this for every offending script, each time you encounter this error.
    – mchid
    Commented Oct 5, 2023 at 9:42
73

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.

4
  • 6
    thank you very much... and as a side note you can convert win endings into unix endings by fromdos <filepath> and fromdos utility is in tofrodos package in standard repos.
    – destan
    Commented Feb 26, 2013 at 9:36
  • ubuntu should be smart enough to understand both styles. what year is this again?
    – endolith
    Commented May 20, 2014 at 2:52
  • You can use this in Vim editor to convert file format - :set ff=unix More details in this answer: stackoverflow.com/a/82743/9152071 Commented Jul 18, 2020 at 0: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
    Commented Apr 28, 2022 at 14:02
30

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.

2
  • 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
    Commented Apr 14, 2022 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
    Commented May 3, 2022 at 9:53
22

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

sudo ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python
2
  • it's say file exists Commented Nov 16, 2022 at 9:06
  • When I ran which python3 it came back with /usr/bin/python3 So this didn't work. My solution came from this post Which came from Apple changing the default of python to not have v2.7
    – Shadoath
    Commented Aug 16, 2023 at 12:51
15

For those with macOS or M1 machines (tested on 12.5) symlinking /usr/bin/python3 will not work because it's a shim to the xcode python3 installation. Symlinking against the shim will just result in an endless cycle of being prompted by the xcode installation tool to install required binaries.

Instead try:

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

to bypass the shim and symlink to the python3 binary (technically this is a symlink to several symlinks to the actual python3 binary installed by xcode but it's sufficient since we really just need to bypass the shim).

As this might change in the future, I'll include these readings on how certain binaries are shimmed in macOS:

https://macops.ca/developer-binaries-on-os-x-xcode-select-and-xcrun/ https://randomtechnicalstuff.blogspot.com/2016/05/os-x-and-xcode-doing-it-apple-way.html

that may be of interest to some.

11

Just modify the shebang line

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


  • This is automatically done by sudo apt install python-is-python3, as @mchid answered.

  • Why this works: If you do $ python, it will say Command 'python' not found ..., but if you do $ python3, it should work.

1
  • 2
    This is a better option, assuming you can modify the script easily
    – zymhan
    Commented May 5, 2022 at 21:34
5

creating a symbolic link solved the issue for me

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

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

1
  • 1
    getting error ` Operation not permitted, do i need to create dir called python` first Commented Nov 16, 2022 at 9:10
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.
0

Check if python appears with this command:

dpkg -l | grep '^ii' | grep python

or

ls /usr/bin/python*
ls /usr/local/bin/python*

These commands should check if it is installed and provide you with the Python paths.

sudo update-alternatives --install /usr/bin/python3 python /usr/bin/python3.x 1 
// Alternative 1
sudo ln -s /usr/bin/python3 /usr/bin/python
// Alternative 2
sudo update-alternatives --config python3

replace python3.x with the version you use.

You can also try this configuration with python2, you just need to modify python3.

Not the answer you're looking for? Browse other questions tagged or ask your own question.