DEV Community

Felix Nullbyte
Felix Nullbyte

Posted on

Pyenv Does Not Use the Correct Python Version: Diagnose It

You set a version with pyenv global, or activated a pyenv-virtualenv, and the prompt even changed. Then python -V prints a completely different interpreter — often the system Python. The confusing part is that pyenv which python points at the right binary, so pyenv looks healthy while your shell ignores it.

That split is the clue. This is the classic "pyenv does not use the correct Python version" situation: pyenv reports the right selection while the shell runs something else. Either something is winning the race before the pyenv shim, or a higher-priority setting is overriding pyenv's choice. These five checks find the cause in order, from the cheapest test to the one that requires rebuilding an environment.

Table of Contents

Step 1: Capture Four Facts Before You Change Anything

Run these in the directory where the problem appears, not in your home directory:

python -V
which python
pyenv version
pyenv which python
Enter fullscreen mode Exit fullscreen mode

Each answer narrows the problem:

  • python -V is what actually runs, so it is the symptom.
  • which python (or command -v python) shows which file the shell resolved. If it is not under $(pyenv root)/shims, the shim was never reached.
  • pyenv version shows the version pyenv believes is active and how it was set, such as a .python-version file or the PYENV_VERSION environment variable.
  • pyenv which python asks pyenv directly which real executable it would run, bypassing the shim lookup. This is why the two can disagree. If it prints a path outside $(pyenv root)/versions, pyenv is falling through to the system interpreter because the selected version is missing.

If which is aliased on your machine, use command -v python for a plain path and type -a python to list every match, including aliases and shell functions.

What you see What it means Go to
which python is not $(pyenv root)/shims/python Another directory or shell definition wins before pyenv Step 2
pyenv version shows system or the wrong version pyenv is selecting the wrong version, so PATH is not the issue Step 3
pyenv version is right but python -V is not The selected version is missing, the shim is bypassed, or the shell cached an old path Step 2 and Step 3
A virtualenv prompt is active but pyenv version shows the base version The environment's metadata or activation is inconsistent Step 4

Step 2: Check Shell Initialization and PATH Order

Pyenv does not replace python. It puts a directory of small shim executables at the front of PATH; when you run python, the shim asks pyenv which installed interpreter to use. The pyenv README's explanation of PATH and shims describes how the shim directory is prepended and why the first matching directory in PATH wins, so a real python in an earlier directory silently bypasses pyenv entirely.

Confirm the shim comes first

Print the shim location and the numbered entries in PATH:

echo "$(pyenv root)/shims"
printf '%s\n' "$PATH" | tr ':' '\n' | nl | sed -n '1,15p'
Enter fullscreen mode Exit fullscreen mode

The shim directory must appear before every directory that contains a python binary — for example /opt/homebrew/bin, /usr/local/bin, /usr/bin, a conda bin directory, or ~/.pyenv/versions/<version>/bin. Then ask the shell what it would actually run:

type -a python
Enter fullscreen mode Exit fullscreen mode

If an alias or shell function appears first, it wins no matter what PATH says. If a real binary outside the shims appears first, something prepended that directory after pyenv initialized.

Fix startup-file order

Shell startup files run in order, and whichever file runs last and prepends a Python directory ends up first in PATH. Common culprits are a conda init block, brew shellenv, asdf, a language-specific version manager, and terminal profiles injected by an IDE. Move the pyenv initialization after those blocks, or remove the duplicate Python directory they add.

A subtler version of this problem appears when a child process re-exports a polluted PATH. A 2026 pyenv issue about leaked PATH entries documents a case where a tool captured ~/.pyenv/versions/<version>/bin from a Python subprocess and wrote it back into the parent shell ahead of the shims; after that, pyenv activate sets PYENV_VERSION correctly and the prompt changes, but python keeps resolving to the hardcoded version directory. If you see a versions/<version>/bin entry in PATH, fix or restart the wrapper that put it there rather than editing your pyenv configuration.

Pyenv's current setup uses a single eval line per shell. pyenv init --install writes the recommended startup block, pyenv init --path prints only the shims path, and pyenv init - installs the shims, completions, and shell function together. The shell function is what lets pyenv shell change the current session, so a config that only runs pyenv init --path can enable shims while leaving pyenv shell unable to persist. Use the full form, matched to your shell:

# Bash
eval "$(pyenv init - bash)"

# Zsh
eval "$(pyenv init - zsh)"
Enter fullscreen mode Exit fullscreen mode

For Bash, add the block to both ~/.bashrc and your login profile, because login and interactive shells read different files. If BASH_ENV points at .bashrc, put the initialization in the profile file instead to avoid recursion.

Step 3: Hunt Down Higher-Priority Version Overrides

If the shim is winning and pyenv version still reports the wrong version, pyenv is being told to use it. The pyenv command reference for local and shell notes that pyenv local overrides the global version and that pyenv shell overrides both, which gives you three places to inspect.

PYENV_VERSION in the current shell

PYENV_VERSION has the highest priority. Check it directly:

echo "${PYENV_VERSION-<unset>}"
pyenv shell
Enter fullscreen mode Exit fullscreen mode

If it is set, clear it with pyenv shell --unset, or simply unset PYENV_VERSION when shell integration is unavailable. This variable often leaks from a parent process: Jupyter, an IDE, or a tool launched with pyenv exec can export it into child shells, where pyenv version will report that the version was set by an environment variable. If you are inside such a shell, start a clean terminal or unset the variable before testing again.

A .python-version file in a parent directory

Pyenv searches the current directory and then each parent directory for .python-version, and only then falls back to the global version file. The source for the pyenv version-file command shows that upward search and the final fallback to $PYENV_ROOT/version. You do not have to guess which file is responsible:

pyenv version-file
Enter fullscreen mode Exit fullscreen mode

That prints the nearest .python-version that applies, or the global version file if no local file exists. A forgotten ~/.python-version or a project-level file several directories up is a common cause of a version that seems to change by itself.

To remove a local file that pyenv version-file names, cd into its directory and run:

pyenv local --unset
Enter fullscreen mode Exit fullscreen mode

Running that from a subdirectory removes only that subdirectory's file. If the command named the global $PYENV_ROOT/version file instead, change the global selection with pyenv global. Also confirm that the version in play is still installed with pyenv versions; a stale .python-version that points at an uninstalled version makes pyenv version report that the version is not installed, while the shim's search falls through to the system interpreter, so python quietly runs system Python.

A cached command path

Bash remembers the full path of commands it has already found. The Bash manual's description of hash explains that hash stores those locations and that hash -r forgets them. If you changed PATH or replaced a binary during the same session, clear the cache before concluding anything:

hash -r        # Bash
rehash         # Zsh
Enter fullscreen mode Exit fullscreen mode

Assigning to PATH also clears Bash's cache, but an explicit reset removes doubt and takes a second.

Step 4: Verify a pyenv-virtualenv's Real Interpreter

A virtualenv can activate, change your prompt, and still run the wrong interpreter. That happens when the environment's bin/python or its recorded base interpreter does not match the version in its name.

Start by checking the active environment from the inside:

pyenv version
pyenv which python
python -c 'import sys; print(sys.executable); print(sys.prefix); print(sys.base_prefix)'
Enter fullscreen mode Exit fullscreen mode

Inside a working virtualenv, sys.prefix and sys.base_prefix differ. Python's venv documentation explains that this inequality is the reliable test for whether the running interpreter is inside an environment, and that activation works by prepending the environment's bin directory to PATH.

Read the environment's configuration

Ask pyenv where the environment lives, then read its configuration file:

pyenv prefix myenv
cat "$(pyenv prefix myenv)/pyvenv.cfg"
Enter fullscreen mode Exit fullscreen mode

The same venv documentation describes the home key in pyvenv.cfg as the Python installation from which the environment was created. If home points at /usr/bin, at a Homebrew Python, or at a different pyenv version than the environment name suggests, the name is misleading even though activation succeeds.

You can also list every environment with its source version:

pyenv virtualenvs
Enter fullscreen mode Exit fullscreen mode

The pyenv-virtualenv README shows output such as venv34 (created from /home/you/.pyenv/versions/3.4.3), with an asterisk marking the active environment. If the "created from" path is not the interpreter you expect, rebuild rather than trying to patch the environment in place.

Rebuild an environment whose name lies

Delete and recreate it with an explicit base version:

pyenv virtualenv-delete myenv
pyenv virtualenv 3.12.4 myenv
pyenv local myenv
Enter fullscreen mode Exit fullscreen mode

pyenv virtualenv-reset myenv also deletes and recreates an environment under the same name, but it reuses the original source version, which may be system if the environment was created outside pyenv. When the base interpreter is the thing you distrust, an explicit delete and create is safer. The venv documentation also notes that environments are not meant to be moved or copied, so recreating is the supported fix for a relocated or broken environment.

Finally, make sure the project's .python-version contains the environment name (myenv), not the base version. Auto-activation only triggers when the file names a valid environment; pyenv local myenv writes the correct value.

Step 5: Confirm the Fix From a Fresh Shell

Test in a shell that has not inherited your debugging session. Open a new terminal, or replace the current shell with exec "$SHELL"; if your configuration lives in a login-only file, log out and back in.

Then run this single check from the project directory:

[ "$(command -v python)" = "$(pyenv root)/shims/python" ] && pyenv which python | grep -q "^$(pyenv root)/versions/" && [ "$(python -c 'import sys; print(sys.executable)')" = "$(pyenv which python)" ] && python -V
Enter fullscreen mode Exit fullscreen mode

It succeeds only when all four conditions hold: the shell resolves python to the shim, pyenv's chosen executable is a real file under versions/ rather than a system fallback, the running interpreter is that exact executable, and python -V prints the version. If it fails, the failing condition points back to Step 2, Step 3, or Step 4.

After installing a new Python version or a package that adds console scripts, run pyenv rehash so new shims are generated. Then repeat the check once more; a version that works immediately after activation but breaks in a new terminal almost always points back to shell initialization order.

Commands to Avoid When pyenv Manages Python

When pyenv is in charge, resist fixes that move a specific interpreter to the front of PATH permanently. Editing /etc/paths on macOS, adding ~/.pyenv/versions/<version>/bin to a profile, symlinking /usr/local/bin/python3 to a pyenv build, or force-linking a Homebrew Python with brew link --force all bypass the shims. They may make one project work while silently breaking per-directory version switching everywhere else. For a one-off command, use pyenv exec or pyenv do <version> <command> instead of changing global PATH.

Do not remove the operating system's Python. The Python documentation for macOS states that /usr/bin/python3 belongs to Apple's Xcode or Command Line Tools, is used by Apple and third-party software, and should never be modified or deleted. The same caution applies to the distro-managed python3 on Linux, which system packages depend on. Installing pyenv alongside those interpreters is fine; replacing them is not.

If you also have Python from Homebrew, python.org, or a distro package and want the wider picture of how installation and switching differ by platform, this guide to switching Python versions safely on Windows, macOS and Linux covers the per-OS basics. Use it for background, but keep pyenv's shims in charge of the projects that rely on pyenv.

Conclusion

A pyenv version mismatch is usually not a broken pyenv installation. Check the facts first: python -V, which python, pyenv version, and pyenv which python. If pyenv's own answer is correct but the shell disagrees, the shim is being bypassed — look at PATH order and startup files. If pyenv's answer is wrong, look for PYENV_VERSION, a parent .python-version, or a stale environment. For virtualenvs, read pyvenv.cfg and rebuild when the recorded base interpreter does not match the name. Confirm everything from a fresh shell with the one-line test, and leave the system Python alone.

Top comments (0)