/usr/bin/env: 'python': No such file or directory means the script's first
line asks for a program called python, and Ubuntu 24.04 with Python
installed has only python3. Change the first line to
#!/usr/bin/env python3, or install python-is-python3 to add a
python command.
Editing the shebang fixes this one script and needs nothing installed. The package adds a
python command for the whole system without editing the script; both were
tested below.
The error
docker exec fix10-lc-r1 bash -c 'cd /root; chmod +x hello.py; ./hello.py 2>&1; echo exit=$?'
/usr/bin/env: 'python': No such file or directory
exit=127
Python was installed when this ran; the same container answered:
docker exec fix10-lc-r1 bash -c 'apt-get install -y python3 > /dev/null 2>&1; echo install-exit=$?; python3 --version 2>&1'
install-exit=0
Python 3.12.3
Why it happens
The script's first line is #!/usr/bin/env python, and the message comes
from /usr/bin/env, naming python. After installing the python3 package there was a
python3 command but still no python, so env found nothing and printed the message
with exit code 127. The script itself is fine: python3 hello.py ran it
without a change.
docker exec fix10-lc-r1 bash -c 'cd /root; head -1 hello.py; python3 hello.py 2>&1'
#!/usr/bin/env python
Hello from Maria Garcia's report script
In a container the first step can be one earlier still. A fresh container from
ubuntu:24.04 had no Python of either name, and the same script failed there
with the same message:
docker exec fix10-lc-r7 bash -c 'ls -l /usr/bin/python /usr/bin/python3 2>&1; command -v python python3; echo exit=$?'
ls: cannot access '/usr/bin/python': No such file or directory
ls: cannot access '/usr/bin/python3': No such file or directory
exit=1
docker exec fix10-lc-r7 bash -c 'cd /root; chmod +x hello.py; head -1 hello.py; ./hello.py 2>&1; echo exit=$?'
#!/usr/bin/env python
/usr/bin/env: 'python': No such file or directory
exit=127
There, install python3 first, then apply one of the fixes below.
The fix
Option one, the package that adds the python name:
docker exec fix10-lc-r1 bash -c 'apt-get install -y python-is-python3 > /dev/null 2>&1; echo install-exit=$?; ls -l /usr/bin/python 2>&1'
install-exit=0
lrwxrwxrwx 1 root root 7 Jun 13 2023 /usr/bin/python -> python3
docker exec fix10-lc-r1 bash -c 'cd /root; ./hello.py 2>&1; echo exit=$?'
Hello from Maria Garcia's report script
exit=0
Option two, with that package removed again, the shebang changed to python3:
docker exec fix10-lc-r1 bash -c 'apt-get remove -y python-is-python3 > /dev/null 2>&1; echo remove-exit=$?; cd /root; sed -i ''1s/python$/python3/'' hello.py; head -1 hello.py; ./hello.py 2>&1; echo exit=$?'
remove-exit=0
#!/usr/bin/env python3
Hello from Maria Garcia's report script
exit=0
python-is-python3 is a symbolic link, /usr/bin/python pointing
to python3, so scripts you did not write work unchanged. For a script you
own, the python3 shebang is the smaller change and keeps working on a
machine without that package.
How it was reproduced
The capture was made in an ubuntu:24.04 container (Ubuntu 24.04.5 LTS, bash
5.2.21) on Docker Engine 29.9.3, after apt-get update and
apt-get install -y python3 (Python 3.12.3). A two-line
hello.py with #!/usr/bin/env python was copied in with
docker cp, made executable and run; then each fix was applied and the script
run again. A second, fresh container from the image (fix10-lc-r7) had neither
python nor python3.
The fix10-lc-r1 container name is the test's own.
Every command was sent from Windows PowerShell 5.1 exactly as shown, with the -c text in single quotes so it reached the container unchanged; a single quote inside that text is written twice, as PowerShell requires.
Frequently asked
- How do I fix /usr/bin/env: 'python': No such file or directory?
- Change the script's first line to #!/usr/bin/env python3, or install python-is-python3. Both made the test script run with exit code 0.
- What does python-is-python3 do?
- It adds /usr/bin/python as a symbolic link to python3. In the test ls -l showed /usr/bin/python -> python3 after the install.
- Why is there no python command on Ubuntu 24.04?
- Installing python3 gave a python3 command but no python. In the test python3 --version printed Python 3.12.3 while env could not find python.
More decoded errors in the Fixes category. If apt
refuses a package called python, see
Package 'python' has no installation candidate.