System python version active instead of local pyenv version

Solution 1:

I will try to give you a thorough walk-through.

How pyenv global/local is used to resolve python

  • I am working on an empty folder ~/py-version-example/
  • I use pyenv (version 1.2.26) to manage all the multiple installations of Python in my system
py-version-example $ pyenv --version
pyenv 1.2.26
  • Currently, I have the following versions of Python available to be "set" by pyenv in my projects
  • Version 3.9.2 is configured as the default by pyenv global
  • This version number is stored in the "global" file ~/.pyenv/version
py-version-example $ pyenv versions
  system
* 3.9.2 (set by /Users/***/.pyenv/version)
  3.9.3
  3.9.4
  miniforge3-4.9.2
  • Since this is a brand new project, no local version of Python has been set by pyenv
  • However, as per pyenv global above, the version of Python "visible" will be 3.9.2
  • This version of the Python interpreter is resolved by the shims installed and managed by pyenv
py-version-example $ pyenv local
pyenv: no local version configured for this directory

py-version-example $ pyenv global
3.9.2

py-version-example $ python -V
Python 3.9.2

py-version-example $ which python
/Users/***/.pyenv/shims/python

py-version-example $ pyenv version
3.9.2 (set by /Users/***/.pyenv/version)

Set up new project

  • Now I am going to set the local version of Python to 3.9.4.
  • You will see that the exact same shim will be used (/Users/***/.pyenv/shims/python)
  • However, this time the version will from the file .python-version inside the project folder:
py-version-example $ pyenv local 3.9.4

py-version-example $ pyenv local
3.9.4

py-version-example $ python -V
Python 3.9.4

py-version-example $ which python
/Users/***/.pyenv/shims/python

py-version-example $ pyenv version
3.9.4 (set by /Users/***/py-version-example/.python-version)

Set up virtual environment

  • Now I am going to set up and activate a new virtual environment inside my project folder
  • Per all items described above, this virtual environment will "inherit" version 3.9.4 from local
  • However, this time you should see that the Python interpreter comes from the virtual environment, and no longer from the pyenv shim:
py-version-example $ python -m venv .venv

py-version-example $ source .venv/bin/activate

(.venv) py-version-example $ python -V
Python 3.9.4

(.venv) py-version-example $ which python
/Users/***/py-version-example/.venv/bin/python