Fix: python: command not found (or python3: No such file or directory)
The Error
You type python or python3 in your terminal and get:
bash: python: command not foundOr one of these variations:
bash: python3: command not found
zsh: command not found: python
/usr/bin/env: 'python': No such file or directory
-bash: /usr/bin/python: No such file or directory
Command 'python' not found, did you mean:
command 'python3' from deb python3Or Python runs, but it’s the wrong version:
$ python --version
Python 2.7.18 # Expected 3.12Why This Happens
Your shell can’t find a python (or python3) executable in any directory listed in your PATH environment variable.
The most common causes:
- Python isn’t installed. Minimal Linux installs and some Docker images ship without Python.
- The command is
python3, notpython. Most modern Linux distributions only providepython3and don’t create apythonsymlink. This is intentional — PEP 394 recommends thatpythonbe left undefined on systems where Python 2 has been removed, to avoid ambiguity. - Python is installed but not in your PATH. This is extremely common on Windows and when using pyenv, Homebrew, or Conda without completing the setup.
- You installed a new Python version but the shell still caches the old one. Your shell hashes command locations. After installing or reinstalling Python, the hash table may point to a stale path.
- Your virtual environment isn’t activated. The
pythoncommand inside a venv only works when the venv is active.
First, figure out what you have:
which python
which python3
python --version
python3 --versionIf which returns nothing, Python is either not installed or not in your PATH. If it returns a path but the version is wrong, you have a symlink or PATH ordering issue.
Fix 1: Install Python
If Python is genuinely not installed, install it.
Ubuntu / Debian
sudo apt update
sudo apt install python3This installs Python 3 and the python3 command. If you also want the python command to point to Python 3:
sudo apt install python-is-python3The python-is-python3 package creates a symlink from python to python3. Available on Ubuntu 20.04+ and Debian 11+.
Fedora / RHEL / CentOS / AlmaLinux
sudo dnf install python3On RHEL 8+ and CentOS Stream, a specific version may be required:
sudo dnf install python3.12Arch Linux
sudo pacman -S pythonArch names the package python and it provides both python and python3 commands.
macOS
macOS no longer ships with Python as of macOS 12.3 (Monterey). Install it via Homebrew:
brew install python@3.12Homebrew installs Python 3 and creates both python3 and python symlinks in its bin directory (e.g., /opt/homebrew/bin/ on Apple Silicon, /usr/local/bin/ on Intel).
If python still doesn’t work after installing via Homebrew, see Fix 4: Fix Your PATH.
Windows
Download the installer from python.org. During installation, check the “Add Python to PATH” checkbox on the first screen. This is unchecked by default and is the single most common cause of python: command not found on Windows.
If you already installed Python without checking that box, see Fix 5: Fix Your PATH on Windows.
You can also install via the Microsoft Store:
winget install Python.Python.3.12Or via the Windows Store, which automatically handles PATH.
Fix 2: Use python3 Instead of python
On most Linux distributions, the command is python3, not python. There is no python command by default.
python3 --version
python3 script.py
python3 -m pip install requestsIf you’re running a script that starts with #!/usr/bin/env python and fails, update the shebang to:
#!/usr/bin/env python3If you can’t modify the script and need python to exist, create the symlink yourself.
Ubuntu / Debian
sudo apt install python-is-python3Manual symlink (any Linux)
sudo ln -s /usr/bin/python3 /usr/bin/pythonWarning: Only do this if you’re sure no legacy Python 2 scripts depend on python pointing to Python 2.
Alias (per-user, non-permanent for scripts)
Add to your ~/.bashrc or ~/.zshrc:
alias python=python3
alias pip=pip3Then reload:
source ~/.bashrcNote: Aliases only work in interactive shells. Scripts, cron jobs, and other non-interactive contexts won’t see them. For those, use a symlink or update-alternatives.
Fix 3: Use update-alternatives (Multiple Python Versions on Linux)
If you have multiple Python versions installed and want to control which one python3 (or python) points to, use update-alternatives:
# Register Python versions
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2
# Choose the default
sudo update-alternatives --config python3This shows an interactive menu where you select the version. The higher priority number (second argument) is the automatic default.
To also set python (not just python3):
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 2
sudo update-alternatives --config pythonVerify:
python --version
python3 --versionInstalling additional Python versions on Ubuntu/Debian
The deadsnakes PPA provides newer (and older) Python versions for Ubuntu:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12 python3.12-venv python3.12-devThen register it with update-alternatives as shown above.
On Fedora, multiple versions are available directly:
sudo dnf install python3.11 python3.12Fix 4: Fix Your PATH (Linux / macOS)
Python is installed, but your shell can’t find it because its directory isn’t in your PATH.
First, locate Python:
# Find all python executables on the system
find / -name "python3" -type f 2>/dev/null
# or
find / -name "python3*" -type f 2>/dev/null | head -20Common locations:
| Installation Method | Path |
|---|---|
| System (apt/dnf) | /usr/bin/python3 |
| Homebrew (Apple Silicon) | /opt/homebrew/bin/python3 |
| Homebrew (Intel Mac) | /usr/local/bin/python3 |
| pyenv | ~/.pyenv/shims/python |
| Conda / Miniconda | ~/miniconda3/bin/python |
| Anaconda | ~/anaconda3/bin/python |
| Python.org installer (macOS) | /Library/Frameworks/Python.framework/Versions/3.12/bin/python3 |
| Compiled from source | /usr/local/bin/python3 |
Once you find the path, add its directory to your PATH. Edit ~/.bashrc (for bash) or ~/.zshrc (for zsh):
export PATH="/opt/homebrew/bin:$PATH"Reload your shell:
source ~/.bashrc # or source ~/.zshrcHomebrew Python on macOS
After brew install python@3.12, if python3 works but python doesn’t:
brew link python@3.12If that says it’s already linked, Homebrew may have put the symlink in a directory that’s not in your PATH. Check:
brew --prefix python@3.12
ls -la $(brew --prefix python@3.12)/bin/Make sure the Homebrew bin directory is in your PATH. For Apple Silicon Macs, add to ~/.zshrc:
eval "$(/opt/homebrew/bin/brew shellenv)"For Intel Macs:
eval "$(/usr/local/bin/brew shellenv)"macOS: The “xcrun: error” after upgrading macOS
After a macOS upgrade, you might get:
xcrun: error: invalid active developer pathThis happens because the upgrade removed the command line tools. Reinstall them:
xcode-select --installFix 5: Fix Your PATH (Windows)
On Windows, the most common cause is installing Python without checking “Add Python to PATH.”
Option A: Re-run the installer
- Open the Python installer (download from python.org if needed).
- Click Modify.
- Click Next through Optional Features.
- On the Advanced Options screen, check “Add Python to environment variables”.
- Click Install.
Option B: Add to PATH manually
Find where Python is installed. Common locations:
C:\Users\<you>\AppData\Local\Programs\Python\Python312\C:\Python312\C:\Program Files\Python312\
Open System Properties → Environment Variables (search “environment variables” in the Start menu).
Under User variables, select
Pathand click Edit.Add two entries:
C:\Users\<you>\AppData\Local\Programs\Python\Python312\C:\Users\<you>\AppData\Local\Programs\Python\Python312\Scripts\
Click OK and restart your terminal.
Option C: Use the py launcher
Windows installs a py launcher (py.exe) that finds installed Python versions automatically:
py --version
py -3.12 --version
py script.py
py -m pip install requestsThe py launcher works even when python is not in your PATH. It’s installed by default with Python from python.org.
Windows Store alias conflict
Windows 10/11 includes app execution aliases for python.exe and python3.exe that open the Microsoft Store instead of running Python. If typing python opens the Store:
- Go to Settings → Apps → Advanced app settings → App execution aliases.
- Turn off the aliases for
python.exeandpython3.exe.
Fix 6: Set Up pyenv Correctly
pyenv installs and manages multiple Python versions in your home directory. If you installed pyenv but python still shows the system version (or “command not found”), the shell initialization is incomplete.
Install pyenv
curl https://pyenv.run | bashAdd to your shell profile
Add these lines to ~/.bashrc (or ~/.zshrc):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"Restart your shell (close and reopen the terminal, or exec "$SHELL").
Install and set a Python version
pyenv install 3.12
pyenv global 3.12Verify:
python --version # Should show Python 3.12.x
which python # Should show ~/.pyenv/shims/pythonCommon pyenv issues
pyenv: command not found — The pyenv binary isn’t in your PATH. Make sure the export PATH line above is in your shell profile and you’ve restarted the shell.
python shows the system version, not pyenv’s — Run pyenv init and check if the output matches what’s in your shell profile. Also make sure eval "$(pyenv init -)" is near the end of your shell profile, after any lines that modify PATH.
pyenv install fails with build errors — You need build dependencies:
# Ubuntu/Debian
sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev libffi-dev \
liblzma-dev libncurses-dev tk-dev
# Fedora
sudo dnf install gcc make zlib-devel bzip2-devel \
readline-devel sqlite-devel openssl-devel libffi-devel \
xz-devel tk-devel
# macOS
brew install openssl readline sqlite3 xz zlibpyenv with virtual environments
Use pyenv-virtualenv or plain venv:
# Using built-in venv (recommended)
pyenv shell 3.12
python -m venv .venv
source .venv/bin/activate
# Using pyenv-virtualenv plugin
pyenv virtualenv 3.12 my-project
pyenv activate my-projectFix 7: Conda / Miniconda Python Not Found
If you installed Conda or Miniconda but python isn’t found, Conda’s initialization didn’t complete.
Initialize Conda
~/miniconda3/bin/conda init bash # or zsh, fishRestart your shell. You should see (base) in your prompt, indicating the base Conda environment is active.
If you don’t want Conda to activate automatically on every shell start:
conda config --set auto_activate_base falseThen manually activate when needed:
conda activate
python --versionConda vs venv vs pyenv — when to use which
| Tool | Best for |
|---|---|
| venv | Per-project isolation. Built into Python 3. No extra install needed. |
| pyenv | Managing multiple Python versions. Lightweight. |
| Conda | Data science. When you need non-Python dependencies (CUDA, MKL, C libraries). |
| pyenv + venv | Managing multiple Python versions AND per-project isolation. |
You don’t need all three. For most web development, pyenv + venv (or just venv with system Python) is sufficient. For data science with GPU dependencies, Conda is the practical choice.
Fix 8: Docker — Python Not Found in Containers
Use the right base image
If your Dockerfile uses alpine, ubuntu, or debian as the base, Python is not included:
# Python is NOT included:
FROM alpine:3.19
FROM ubuntu:24.04
FROM debian:12-slim
# Python IS included:
FROM python:3.12-slim
FROM python:3.12-alpine
FROM python:3.12If you need Python in a non-Python base image:
# Alpine
FROM alpine:3.19
RUN apk add --no-cache python3 py3-pip
# Ubuntu/Debian
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y --no-install-recommends python3 python3-pip && rm -rf /var/lib/apt/lists/*python vs python3 inside containers
Many Docker images only have python3, not python. If your entrypoint or script uses python, either update it to python3 or create a symlink in your Dockerfile:
RUN ln -s /usr/bin/python3 /usr/bin/pythonMulti-stage builds
A common issue: you install Python in a build stage but it’s missing in the final stage:
# Build stage
FROM python:3.12-slim AS builder
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
# Final stage — make sure Python exists here too
FROM python:3.12-slim
COPY --from=builder /install /usr/local
COPY . .
CMD ["python", "app.py"]Fix 9: Fix Shebang Lines
If a Python script fails with No such file or directory or command not found when run directly (./script.py), the shebang line is wrong.
Bad shebangs:
#!/usr/bin/python # Python 2 or doesn't exist
#!/usr/local/bin/python3 # Only works if Python is at this exact path
#!/usr/bin/python3.10 # Breaks when you upgrade to 3.12Good shebangs:
#!/usr/bin/env python3 # Portable — finds python3 wherever it is in PATH/usr/bin/env searches your PATH for the command, so it works across systems where Python is installed in different locations.
Windows line endings break shebangs. If the script was created or edited on Windows, it may have \r\n line endings. The system tries to find python3\r, which doesn’t exist:
# Check
file script.py
# Fix
dos2unix script.py
# Or without dos2unix:
sed -i 's/\r$//' script.pyFix 10: Rehash Your Shell
After installing or upgrading Python, your shell may still cache the old location (or lack thereof). This is called the command hash table.
# Bash: clear the hash
hash -r
# Zsh: clear the hash
rehash
# Or just restart your terminalIf you use pyenv:
pyenv rehashThis rebuilds the pyenv shim files for all installed Python versions and packages.
Still Not Working?
python works but pip doesn’t
The pip command may not be installed or may be a separate package:
# Ubuntu/Debian
sudo apt install python3-pip
# Fedora
sudo dnf install python3-pip
# Any system — use the module directly (always works if Python is installed)
python3 -m pip install requestsUsing python3 -m pip instead of bare pip guarantees you’re using pip for the correct Python version. Make this your default habit. If you’re getting externally-managed-environment errors from pip, see Fix: error: externally-managed-environment (pip install).
Virtual environment python doesn’t work
If you created a venv and later upgraded or reinstalled Python, the venv’s python symlink points to a binary that no longer exists:
bash: /home/user/project/.venv/bin/python: No such file or directoryRecreate the venv:
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtIf python3 -m venv itself fails, install the venv module:
sudo apt install python3-venv # Ubuntu/Debian
sudo dnf install python3-venv # Fedorawhich python3 shows the right path but it doesn’t run
The binary might be corrupted or missing dependencies. Verify:
file $(which python3)
ldd $(which python3) # Check for missing shared librariesIf ldd shows not found for any library, the Python installation is broken. Reinstall:
sudo apt install --reinstall python3Cron jobs can’t find Python
Cron runs with a minimal PATH (usually just /usr/bin:/bin). If Python is elsewhere, cron can’t find it. Use the full path in your crontab:
# Wrong
* * * * * python3 /home/user/script.py
# Correct — use the full path
* * * * * /usr/bin/python3 /home/user/script.py
# Or for pyenv-managed Python
* * * * * /home/user/.pyenv/shims/python /home/user/script.pyOr set PATH at the top of your crontab:
PATH=/home/user/.pyenv/shims:/usr/local/bin:/usr/bin:/bin
* * * * * python3 /home/user/script.pySSH sessions use a different Python than your interactive shell
Login shells and non-login shells read different config files. SSH non-interactive commands may not source ~/.bashrc:
# This might use a different Python than your terminal:
ssh server "python3 --version"Put your PATH modifications in ~/.profile or ~/.bash_profile (read by login shells) rather than only in ~/.bashrc (read by interactive non-login shells). Or source bashrc explicitly:
ssh server "source ~/.bashrc && python3 --version"Systemd services can’t find Python
Systemd services run in a clean environment without your shell profile. Specify the full path in the service file:
[Service]
ExecStart=/usr/bin/python3 /opt/myapp/app.py
# Or for a venv:
ExecStart=/opt/myapp/.venv/bin/python /opt/myapp/app.pyDon’t rely on PATH in systemd. Use absolute paths.
Wrong Python version persists after installing a new one
You installed Python 3.12 but python3 --version still shows 3.10:
# Check what's actually being used
which python3
ls -la $(which python3)
# Check all installed versions
ls /usr/bin/python3*If python3 is a symlink to an older version, update it with update-alternatives (see Fix 3) or use the versioned command directly:
python3.12 --version
python3.12 -m venv .venvWSL can’t find Python
WSL runs Linux, so all the Linux fixes apply. Make sure you’re installing Python inside WSL, not relying on Windows Python:
# Inside WSL:
sudo apt update
sudo apt install python3 python3-pip python3-venvWindows Python (python.exe on the Windows side) can technically be called from WSL via /mnt/c/..., but this is slow, has path compatibility issues, and should be avoided. Install Python natively inside WSL. For more WSL-specific issues, see Fix: bash: ./script.sh: Permission denied.
Related: Fix: ModuleNotFoundError: No module named in Python | Fix: error: externally-managed-environment (pip install) | Fix: pip could not build wheels | Fix: IndentationError: unexpected indent
Related Articles
Fix: ERROR: Could not build wheels / Failed building wheel (pip)
How to fix pip 'ERROR: Could not build wheels', 'Failed building wheel', 'No matching distribution found', and 'error: subprocess-exited-with-error'. Covers missing C compilers, build tools, system libraries, Python version issues, pre-built wheels, and platform-specific fixes for Linux, macOS, and Windows.
Fix: bash: ./script.sh: Permission denied (Linux, macOS, WSL)
How to fix 'Permission denied' errors in bash, including chmod +x for scripts, file ownership with chown, EACCES in Node.js, Docker volume permissions, SELinux, ACLs, and WSL issues.
Fix: SSL certificate problem: unable to get local issuer certificate
How to fix 'SSL certificate problem: unable to get local issuer certificate', 'CERT_HAS_EXPIRED', 'ERR_CERT_AUTHORITY_INVALID', and 'self signed certificate in certificate chain' errors in Git, curl, Node.js, Python, Docker, and more. Covers CA certificates, corporate proxies, Let's Encrypt, certificate chains, and self-signed certs.
Fix: error: externally-managed-environment (pip install)
How to fix the 'error: externally-managed-environment' and 'This environment is externally managed' error when running pip install on Python 3.11+ on Ubuntu, Debian, Fedora, and macOS.