Skip to main content

Error Solved - pyenv python command not found

Β· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The pyenv: python: command not found error occurs when pyenv is not properly configured to manage your Python versions. The solution is to ensure your shell's configuration files are correctly updated to point to pyenv's shims, which are small executable files that intercept commands like python and pip and redirect them to the correct Python version [4, 5].


Step 1: Verify pyenv Installation πŸ§β€‹

First, confirm that pyenv itself is installed and accessible. Open a new terminal and run:

pyenv --version

If this command returns a version number, then pyenv is installed, but your shell environment is likely not configured correctly. If you get a "command not found" error for pyenv itself, you need to reinstall and configure it properly.


Step 2: Configure Your Shell Environment βš™οΈβ€‹

This is the most common reason for the python: command not found error. You must add pyenv's initialization commands to your shell's startup file.

For most modern macOS systems, the default shell is zsh, and the configuration file is ~/.zshrc. If you're using Bash, the file is ~/.bash_profile or ~/.bashrc.

Add the following lines to the end of your shell configuration file:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

These lines do the following:

  • export PYENV_ROOT="$HOME/.pyenv": Defines the root directory for pyenv.
  • export PATH="$PYENV_ROOT/bin:$PATH": Adds the pyenv executable to your PATH, allowing you to run pyenv commands from anywhere [5].
  • eval "$(pyenv init -)": Initializes pyenv's shims, which are what allow pyenv to manage your python and pip commands [2].

After adding these lines, you must restart your terminal or source the file for the changes to take effect:

source ~/.zshrc

Step 3: Set a Python Version πŸ”’β€‹

Even after correctly configuring your shell, pyenv won't manage a Python version until you've explicitly told it which one to use. This is a crucial step that many people miss.

  1. List installed versions: Check which Python versions you have installed.

    pyenv versions
  2. Set a global version: Choose one of the installed versions to be the default for your system. This creates a ~/.pyenv/version file [4, 5].

    pyenv global 3.12.1

    Alternatively, you can set a local version for your current project, which creates a .python-version file in the current directory.

    pyenv local 3.11.3
  3. Verify the change: The python command should now point to the version you just set.

    python --version
    # Output: Python 3.12.1

Troubleshooting Common Issues πŸ›β€‹

  • pyenv versions is empty: If you haven't installed a Python version yet, you'll see a list of available versions with pyenv install --list. You must install a version before setting it.
  • Restarting Terminal doesn't work: This can happen if your shell's startup files are not configured to load pyenv in the correct order. The eval "$(pyenv init -)" command must run to configure the shims before other path-related configurations [3].

Sources​

  1. GitHub. "pyenv/pyenv". https://github.com/pyenv/pyenv
  2. Real Python. "Managing Multiple Python Versions With pyenv". https://realpython.com/intro-to-pyenv/
  3. Kolibri developer documentation. "Installing pyenv". https://kolibri-dev.readthedocs.io/en/develop/howtos/installing_pyenv.html
  4. DOCSAID. "Managing Python Versions with pyenv". https://docsaid.org/en/blog/pyenv-installation/
  5. Stack Overflow. "pyenv: python :command not found". https://stackoverflow.com/questions/51863225/pyenv-python-command-not-found