VMHeaven

Troubleshooting

python: command not found — fix it on Linux, macOS and in Docker

Modern distros ship python3 but no python command. Fix 'python: command not found' on Debian, Ubuntu, Rocky, Alpine, macOS and Docker without breaking apt.

Published ~9 min read

Quick fix: run python3 --version. If it prints a version, Python is installed and only the name python is missing: call python3, or install the alias package (python-is-python3 on Debian and Ubuntu, python-unversioned-command on Rocky, AlmaLinux and RHEL). If python3 is not found either, install the python3 package for your system.

bash: python: command not found and zsh: command not found: python rarely mean what they seem to. Since Python 2 was retired, Debian 12 and 13 and Ubuntu 22.04 and 24.04 install the interpreter as python3 and create no command called python unless you ask for one. macOS dropped /usr/bin/python in Monterey 12.3, and the plain debian and ubuntu Docker images ship neither name. So the first question is not how to install Python, but whether the interpreter is missing or only its old name is.

Is Python missing, or only the name python?

Three commands answer that and tell you which system you are on:

diagnose
command -v python python3      # prints a path for each name that exists
ls -l /usr/bin/python*         # what is actually installed
grep -E '^(ID|VERSION_ID)=' /etc/os-release
  • Only python3 prints a path — the interpreter is there, the name is not. Use python3, or add the alias for your distribution below.
  • Neither prints anything — Python is not installed. Install the python3 package.
  • python points into your home directory (~/.pyenv/shims, ~/miniconda3, a .venv) — a version manager or an environment owns the name. See pyenv and conda.

Installed something and the shell still says not found? It may have cached the old lookup. Run hash -r in bash or rehash in zsh, or open a new terminal.

How do I install Python on Debian and Ubuntu?

Every supported release packages Python 3 as python3: Debian 12 ships 3.11, Debian 13 ships 3.13, Ubuntu 22.04 ships 3.10 and Ubuntu 24.04 ships 3.12. Cloud images normally have it already, because cloud-init is written in Python. Minimal installs and containers often do not.

Debian · Ubuntu
sudo apt update
sudo apt install -y python3

# optional: make "python" run python3, for every user
sudo apt install -y python-is-python3

python --version

python-is-python3 is little more than two symlinks, /usr/bin/python and /usr/bin/pydoc, pointing at their Python 3 counterparts. Because the package manager owns them, they are updated and removed cleanly, which a link you make by hand is not.

A freshly installed VPS is the classic place to meet this error: the cloud image brings python3 for cloud-init, but no python. If you want to try a fix before touching a server that matters, a small Standard KVM VPS is a cheap, clean sandbox, and a reinstall from the panel puts it back to a fresh system. Deploying a discord.py or aiogram bot? Bot hosting walks through the whole setup, from the virtual environment to the systemd unit.

How do I get python on Rocky Linux, AlmaLinux and RHEL?

A full install of version 9 or 10 already has python3, because dnf itself is written in Python. Minimal container images that ship microdnf instead may not. The unversioned name comes from a separate package in AppStream:

Rocky · AlmaLinux · RHEL 9 and 10
sudo dnf install -y python3 python-unversioned-command
python --version     # 3.9 on version 9, 3.12 on version 10

python-unversioned-command points python at the default python3 and nothing else, even if you add python3.11 or python3.12 from AppStream later; call those by their versioned names. Do not create /usr/bin/python with ln -s on these systems. The package owns that path, and a hand-made link is invisible to dnf. Rocky and Alma 8 work differently: there the switch is sudo alternatives --set python /usr/bin/python3.

How do I install Python on Alpine Linux?

Alpine
apk add --no-cache python3
python --version

Alpine needs no alias step. Its python3 package (checked on 3.20, 3.21 and 3.22) installs /usr/bin/python next to /usr/bin/python3, so both names work as soon as the package is in.

How do I get python on macOS?

Apple removed its bundled Python 2.7, and with it /usr/bin/python, in macOS 12.3. What remains is /usr/bin/python3, a stub that offers to install the Command Line Tools the first time you run it. Either let it, or use Homebrew:

macOS
# Apple's python3, from the Command Line Tools
xcode-select --install
python3 --version

# or Homebrew's
brew install python
python3 --version

Homebrew puts python3 and pip3 on your PATH and keeps the unversioned python and pip links in a separate directory. To use them, put that directory first in your PATH in ~/.zshrc:

~/.zshrc
export PATH="$(brew --prefix python)/libexec/bin:$PATH"

An alias python=python3 in the same file is the lighter option, but aliases only exist in your interactive shell. Scripts with a python shebang still fail.

Why does my script say /usr/bin/env: python: No such file or directory?

Because its first line, the shebang, asks for a program called python. The message depends on how that line is written:

the three variants
# shebang is  #!/usr/bin/env python
/usr/bin/env: 'python': No such file or directory

# shebang is  #!/usr/bin/python
bash: ./tool.py: /usr/bin/python: bad interpreter: No such file or directory

# file was saved with Windows (CRLF) line endings
/usr/bin/env: 'python\r': No such file or directory

If the script runs on Python 3, which almost everything maintained today does, change its first line to #!/usr/bin/env python3. When it is not your script, or there are many of them, install the alias package for your distribution instead. The third message is different: the system is looking for a program literally named python followed by a carriage return, so strip the Windows line endings before anything else:

fix line endings · fix the shebang
# remove Windows line endings first, if the file has them
sed -i 's/\r$//' tool.py        # or: dos2unix tool.py

# point a python shebang at python3 (GNU sed; on macOS use: sed -i '')
sed -i '1s/^#!\(.*\)python$/#!\1python3/' tool.py

If the next error is Permission denied, the file is not executable — that one is covered in bash: Permission denied.

Does a virtual environment give me a python command?

Yes. A virtual environment creates its own python and pip commands, whatever the system calls its interpreter. For project work it is the cleanest fix of all. It is also how you use pip on Debian 12 and later and Ubuntu 23.04 and later, where installing packages into the system Python is refused with error: externally-managed-environment.

any Linux · macOS
# Debian/Ubuntu need the venv module first
sudo apt install -y python3-venv

python3 -m venv .venv
source .venv/bin/activate
python --version      # works inside the environment

Without python3-venv, Debian and Ubuntu stop with ensurepip is not available and tell you which package to install.

How do I get python in a Docker image?

The debian and ubuntu images, including Debian’s -slim tags, contain no Python at all. If the container exists to run Python, start from the official image, which already has python, python3 and pip:

Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

If Python is only a helper in an image built on another base, install it with the distribution’s package manager and clean up in the same layer:

Dockerfile · Debian or Ubuntu base
RUN apt-get update \
 && apt-get install -y --no-install-recommends python3 python-is-python3 \
 && rm -rf /var/lib/apt/lists/*
Dockerfile · Alpine base
RUN apk add --no-cache python3

Why does pyenv or conda say python is not found?

pyenv prints its own version of the error:

pyenv
pyenv: python: command not found

The `python' command exists in these Python versions:
  3.12.4

pyenv found an interpreter, but none is selected for this shell. Pick one for your user or for the current project:

pyenv
pyenv versions          # what is installed
pyenv global 3.12.4     # default for your user
pyenv local 3.12.4      # this directory only (writes .python-version)

If pyenv itself is not found, or python ignores it, the shims are not on your PATH: the eval "$(pyenv init -)" line from pyenv’s setup instructions is missing from ~/.bashrc or ~/.zshrc. Node users know the same pattern from npx: command not found and nvm.

With conda, python points into an environment only while one is active. If the conda command itself is missing, the shell was never initialised for it — see conda: command not found.

Why does python work as my user but not with sudo?

sudo does not keep your PATH. On Debian, Ubuntu and RHEL it replaces it with secure_path from /etc/sudoers, which lists system directories only, not ~/.pyenv/shims, a conda environment or an activated virtual environment. So sudo python finds nothing, or a different interpreter than plain python. Pass the full path instead:

sudo
sudo "$(command -v python)" script.py

# or name the environment's interpreter directly
sudo /opt/app/.venv/bin/python script.py

If it is sudo itself that is missing, that is a different fix: sudo: command not found.

How do I use a different Python version than the system one?

Install it next to the system interpreter, never over it. On Rocky and Alma 9, AppStream carries python3.11 and python3.12. Elsewhere, pyenv builds any version into your home directory, uv python install 3.12 downloads a prebuilt one, and in containers the official python:3.x images pin it for you. Then call it by its versioned name, or create a virtual environment from it:

any distribution
python3.12 -m venv .venv && source .venv/bin/activate

Why does Windows say “Python was not found”?

The full message is Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases. It comes from an App Execution Alias: a placeholder python.exe that Windows keeps on the PATH until a real Python is installed. Install Python from python.org or from the Microsoft Store and open a new terminal. If a real Python is installed and the placeholder still answers, search Settings for Manage app execution aliases and switch off the entries for python.exe and python3.exe. The py launcher that the python.org installers add works even when python does not: py --version.

Frequently asked

Should I symlink python to python3?

Install the package that does it instead: python-is-python3 on Debian and Ubuntu, python-unversioned-command on Rocky, AlmaLinux and RHEL. A hand-made link is invisible to the package manager. Never repoint /usr/bin/python3 itself — system tools depend on it.

Why is there no python command on Ubuntu 24.04?

Ubuntu dropped Python 2 and, with it, the unversioned name, so 'python' can never silently mean a different major version. The interpreter is python3 (Python 3.12 on 24.04); 'sudo apt install python-is-python3' adds the python command.

Can I still install Python 2?

Not from Debian 12, Debian 13 or Ubuntu 24.04, which no longer package it. Ubuntu 22.04 still has a python2 package in its universe repository. Python 2 has had no security fixes since 2020, so port the code to Python 3 or keep legacy scripts isolated.

Why does python work as my user but not with sudo?

sudo replaces your PATH with secure_path from /etc/sudoers, which does not include ~/.pyenv/shims, conda environments or a virtualenv. Run the interpreter by its full path, for example: sudo "$(command -v python)" script.py.

Related articles

All troubleshooting