System python version active instead of local pyenv version
Asked Answered
H

1

1

How can I check if the correct python version is used after activating a virtual environment? I tried which python3 and which python but both outputs indicate the system python version and not the local pyenv version. Is it still using my local version or is there something wrong with my installs?

Here is the command line output.

Documents/DataStuff/pdf_tools via 🐍 v3.9.5
❯ python --version
zsh: command not found: python

Documents/DataStuff/pdf_tools via 🐍 v3.9.5
❯ python3 --version
Python 3.9.5

Documents/DataStuff/pdf_tools via 🐍 v3.9.5
❯ pyenv global
3.8.5

Documents/DataStuff/pdf_tools via 🐍 v3.9.5
❯ pyenv local
3.7.5

Documents/DataStuff/pdf_tools via 🐍 v3.9.5
❯ python3 -m venv .venv

Documents/DataStuff/pdf_tools via 🐍 v3.9.5 took 31s
❯ source .venv/bin/activate
(.venv)  

Documents/DataStuff/pdf_tools via 🐍 v3.9.5 (.venv)
❯ which python
/mnt/c/Users/SaΕ‘a/Documents/DataStuff/pdf_tools/.venv/bin/python
(.venv)  

Documents/DataStuff/pdf_tools via 🐍 v3.9.5 (.venv)
❯ python --version
Python 3.9.5
(.venv)  

Documents/DataStuff/pdf_tools via 🐍 v3.9.5 (.venv)
❯ python3 --version
Python 3.9.5
(.venv)  

Here is some system information:
System: windows-linux-subsystem2
Package manager: Homebrew 3.1.5
Python version: 3.9.5
Pyenv version: 1.2.27

Installation steps:

  1. Install python and pyenv via homebrew.
brew install [email protected]
brew install pyenv
  1. Install python versions 3.7.5 and 3.8.5 via pyenv.
pyenv install 3.7.5
pyenv install 3.8.5
  1. Set pyenv global to 3.8.5 (3.9.5 is not supported yet) and local to 3.7.5
pyenv global 3.8.5
pyenv local 3.7.5
  1. Create a virtual environment
python3 -m venv .venv
  1. Activate the virtual environment
source .venv/bin/activate
Himyaritic answered 10/5, 2021 at 13:40 Comment(0)
H
4

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
Helenhelena answered 16/5, 2021 at 3:9 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.