Error Solved - pyenv python command not found
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 forpyenv
.export PATH="$PYENV_ROOT/bin:$PATH"
: Adds thepyenv
executable to yourPATH
, allowing you to runpyenv
commands from anywhere [5].eval "$(pyenv init -)"
: Initializespyenv
's shims, which are what allowpyenv
to manage yourpython
andpip
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.
-
List installed versions: Check which Python versions you have installed.
pyenv versions
-
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
-
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 withpyenv 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. Theeval "$(pyenv init -)"
command must run to configure the shims before other path-related configurations [3].
Sourcesβ
- GitHub. "pyenv/pyenv".
https://github.com/pyenv/pyenv
- Real Python. "Managing Multiple Python Versions With pyenv".
https://realpython.com/intro-to-pyenv/
- Kolibri developer documentation. "Installing pyenv".
https://kolibri-dev.readthedocs.io/en/develop/howtos/installing_pyenv.html
- DOCSAID. "Managing Python Versions with pyenv".
https://docsaid.org/en/blog/pyenv-installation/
- Stack Overflow. "pyenv: python :command not found".
https://stackoverflow.com/questions/51863225/pyenv-python-command-not-found