Having trouble switching python versions using pyenv global command
Asked Answered
M

3

8

I have installed pyenv on Mac using homebrew and downloaded the version of Python 3.7.9. Everything works except when I use pyenv global 3.7.9, python3 -V still gives me version 3.9.7. How do I fix this?

Minutes answered 19/2, 2022 at 19:45 Comment(2)
Please run pyenv versions and make sure that PyEnv knows about version 3.7.9. I don't remember for sure, but I don't think that just installing a new Python version via brew is enough. I think you have to run some PyEnv command to tell PyEnv that the new version exists. I don't remember just what the syntax is for that. I think PyEnv can install new Python versions itself as well.Neonatal
I typed pyenv install 3.7.9 and it shows that the version is installed right. However the global command still doesn't change the version.Minutes
H
11

Run the commands below in your terminal before running pyenv install <VERSION> / pyenv global <VERSION> / python --version

eval "$(command pyenv init -)"
eval "$(command pyenv init --path)"

You may need to re-install your required python version

To avoid running these commands in every session, add the commands to your shell profile file ~/.zshrc or ~/.bashrc

Entire .zshrc that works as of March 2022:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PIPENV_PYTHON="$PYENV_ROOT/shims/python"

plugin=(
  pyenv
)

eval "$(pyenv init -)"
eval "$(command pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
Hooker answered 5/3, 2022 at 17:51 Comment(1)
why do you use eval?Perilune
P
1

For Arch Linux, these worked, as similarly answered by @Eladio:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PIPENV_PYTHON="$PYENV_ROOT/shims/python"
pyenv init -
command pyenv init --path
Perilune answered 3/5, 2022 at 18:53 Comment(0)
H
0

you could do so by searching the process bin path (which python3 will give you the path of your python3.9.7, which python3.7 will give you the path of your python3.7) and by adding an alias into your ~/.bashrc (assuming you're using in from your terminal) you should be fine

Helpful answered 5/3, 2022 at 11:51 Comment(1)
The whole point about using pyenv is that you can use multiple versions of python automatically, so you are stripping off the auto-part of it, elsewise I am fine with that ;)Perilune

© 2022 - 2024 — McMap. All rights reserved.