3

python points to my system's default python interpreter, instead of my pyenv python interpreter.

I created the python virtual environment and activated it as follows:

pyenv virtualenv 3.8.12 test3
pyenv activate test3

Then, running python gives me a python 3.7 interpreter (which is my system's default python interpreter), instead of 3.8.12. Why?


Full command outputs:

root@server:/home/code-base/f# pyenv virtualenv 3.8.12 test3
Looking in links: /tmp/tmp1yp95sav
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.8.12/envs/test3/lib/python3.8/site-packages (56.0.0)
Requirement already satisfied: pip in /root/.pyenv/versions/3.8.12/envs/test3/lib/python3.8/site-packages (21.1.1)

root@server:/home/code-base/f# pyenv activate test3
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

(test3) root@server:/home/code-base/f# python
Python 3.7.11 (default, Jul 27 2021, 14:32:16)
[GCC 7.5.0] :: Anaconda, Inc. on linux

Additionally:

  • pyenv which python returns /root/.pyenv/versions/test3/bin/python
  • command -v python returns /opt/conda/bin/python
  • $PATH inside my virtualenv: /root/.pyenv/plugins/pyenv-virtualenv/shims:/root/.pyenv/bin:/opt/conda/bin:/app/python/bin:/opt/conda/bin:/usr/local/mpi/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/bin
  • ls -la /root/.pyenv/plugins/pyenv-virtualenv/shims contains two folders: activate and deactivate.
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • What does a `command -v python` return? – Sebastian Mar 05 '22 at 06:06
  • @Sebastian `/opt/conda/bin/python`, which is indeed system's default python 3.7 interpreter. I don't understand as I used `pyenv virtualenv 3.8.12 test3` to create the virtual environment, which means it should use python 3.8.12 from my understanding. – Franck Dernoncourt Mar 05 '22 at 06:08
  • don'thave the full paths, could it be that you have two test3 envs ? – Je Je Mar 05 '22 at 06:18
  • I think you should use `pyenv local` instead of `pyenv virtualenv`. – Xirehat Mar 05 '22 at 06:22
  • @JeJe Thanks, I tested again to create a new python future environment changing test3 to test4, same issue. – Franck Dernoncourt Mar 05 '22 at 06:33
  • What does your `$PATH` look like inside your virtualenv? – Sebastian Mar 05 '22 at 06:37
  • @Sebastian `$PATH` inside my virtualenv: `/root/.pyenv/plugins/pyenv-virtualenv/shims:/root/.pyenv/bin:/opt/conda/bin:/app/python/bin:/opt/conda/bin:/usr/local/mpi/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/bin` – Franck Dernoncourt Mar 05 '22 at 06:38
  • Can you also give us the output of `ls -la /root/.pyenv/plugins/pyenv-virtualenv/shims`? – Sebastian Mar 05 '22 at 07:06
  • @Sebastian `ls -la /root/.pyenv/plugins/pyenv-virtualenv/shims` contains two folders: `activate` and `deactivate`. – Franck Dernoncourt Mar 05 '22 at 07:09

3 Answers3

2
  1. If your ~/.profile sources ~/.bashrc (Debian, Ubuntu, Mint):
sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc

Restart your login session for the changes to profile files to take effect. E.g. if you're in a GUI session, you need to fully log out and log back in.

  1. Install version of python you need.
pyenv install 3.8.12
  1. Activate it on your application directory
pyenv local 3.8.12
  1. Then locate python version
$ pyenv which python
/home/xirehat/.pyenv/versions/3.8.12/bin/python
Xirehat
  • 1,155
  • 1
  • 8
  • 20
2

Given the new informations you gave us it is most likely that your a missing a eval "$(pyenv init --path)" in your ~/.profile (or in your Dockerfile as you are using K8s) as /root/.pyenv/shim is not part of $PATH.

Old answer:

Two possible solutions here:

Either you did not select your 3.8.12 binary as a system default via:

$ pyenv global 3.8.12
$ python -V
Python 3.8.12

$ pyenv versions
  system
  2.7.15
* 3.8.12 (set by /home/realpython/.pyenv/version)

or /opt/conda/bin/ has a higher priority in your $PATH then your pyenv installation.

Sebastian
  • 400
  • 3
  • 9
  • Thanks, I was hoping to use python 3.8 only when my virtual environment is activated, not as my default python interpreter systemwide. – Franck Dernoncourt Mar 05 '22 at 06:31
  • I ran your commands, but python is still 3.7: https://i.stack.imgur.com/ypht3.png – Franck Dernoncourt Mar 05 '22 at 06:52
  • Great catch! Running `eval "$(pyenv init --path)"` before running `python` fixed the issue. Thanks! I did have `eval "$(pyenv init --path)"` in my last line of `~/.bashrc` but that didn't suffice. No idea why. – Franck Dernoncourt Mar 05 '22 at 07:36
2

Once you've created your virtual environment you need to activate it. There's a shell script in the virtual environment's bin directory which you need to source.

For example, my default shell is zsh (on macOS) and in my .zshrc I have the following line:

source "/Volumes/G-DRIVE Thunderbolt 3/PythonStuff/venv3.10.2/bin/activate"

Thus the python executable that will be used in any zsh instance will be based on the venv.

Obviously your path to the venv will be different but you get the point.

DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • Thanks, makes sense, I used to do this for `virtualenv` indeed. I thought that with pyenv, which I'm new to, `pyenv activate test3` would take care of it. – Franck Dernoncourt Mar 05 '22 at 07:50