Set default python with pyenv
Asked Answered
A

7

36

I am try to upgrade my Python version on my macOS Catalina 10.15.1 by install PYENV and PYPIP, and set global and local to version 3.8.0. but still when I try to python version it shows the python version which built into the MacOS operating system. which part is missing?

$ pyenv -v
pyenv 1.2.14

$ pypip -v
zsh: command not found: pypip

$ pyenv versions
  system
* 3.8.0 (set by /Users/aj/.python-version)

$ pyenv global
3.8.0

$ pyenv local
3.8.0

$ python -V
Python 2.7.16
Ankylosis answered 3/11, 2019 at 11:33 Comment(5)
what's the output of type -a python?Veii
$ type -a python python is /usr/bin/pythonAnkylosis
that means your pyenv is not setup properlyVeii
what about the python3 -V command? I think the python command defaults to python 2.x.x on some machines. This is the case for me.Abash
Re: edit -- see Why not upload images of code or errors when asking a question?Kettledrummer
B
52

For me OSX I had to put

eval "$(pyenv init --path)"

inside my ~/.bashrc | ~/.zshrc

note that without the --path it didn't work

Bensen answered 29/9, 2021 at 14:29 Comment(4)
That fixed it for me. Thanks!Chancellor
Thx! This helped me too. Switching from the old pyenv init - principle to pyenv init --path did the trickLindquist
Thank you, --path instead of - on ZSH workedClaudell
Thanks. It worked for me. Just one correction for missing quote eval "$(pyenv init --path)"Spam
V
18

If the output of

type -a python 

is /usr/bin/python, and if there is no second line displayed, then pyenv is only setup partially.

You should have seem as first line something like

/home/username/.pyenv/shims/python

That means your pyenv is not setup properly. It is only set up partially. What's missing is the pyenv shims which redirect to the correct version of python.

Probably your search path contains: /home/username/.pyenv/bin, but it is missing /home/username/.pyenv/shims

(Following comments updated 2021-01-06):

Normally you should have three lines in your ~/.bashrc

The first two (or something equivalent), that you seem to have are:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

On the other hand what you seem to be missing is a line, that looks like:

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

or a more elaborate but in most cases identical line:

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Try to add one of these missing lines to your .bashrc and see whether pyenv is working better.

You could also add ~/.pyenv/shims/python manually to your searchpath, but normally this should have been done by the eval "$(pyenv init -)" command

if ~/.pyenv/shims is already in your search path, then check with

ls ~/.pyenv/shims

whether the directory exists and contains an executable named python. Normally this should have been added latest after having done a pyenv install 3.8.0

Addendum 2022-01-15:

Please note that the way pyenv is initialized changed. If you had an older pyenv version and you updated the cloned repository you have probably something like

eval "$(pyenv init -)"

in your ~/.bash_profile (or your ~/.zshrc or ... depending on your shell)

This has to be changed to something lile

eval "$(pyenv init -)"
eval "$(pyenv init --path)"
Veii answered 3/11, 2019 at 22:51 Comment(3)
Did you try what I suggested?Veii
i had to do this in the .zshrc and it worked, i type echo $0 to find out if my terminal was using the bash_profile or .zshrc filesMitzimitzie
installed py2 in centos stream9 with pyenv but "python" still pointing to py3 after doing pyenv shell 2, after included your last line in my .zshrc (the line with the "--path"), it works like expected: python command opens a py2 version. Thanks!Remorseful
S
13

In case you have multiple versions of python installed (you can use pyenv versions to see all the installed versions), you can set a particular one as local or global version:

$ pyenv versions
  system
* 2.7.18
* 3.7.8
  3.9.9

$ pyenv global 3.9.9

$ pyenv versions
  system
  2.7.18
  3.7.8
* 3.9.9
Scarfskin answered 2/1, 2022 at 23:8 Comment(0)
B
5

As gelondia mentioned, you might need to add this code to your ~/.bash_profile to get your PATH setup correctly:

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

echo $PATH should return something like /Users/matthewpowers/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin. Notice that the ~/.pyenv/shims directory is out front.

I added a separate answer because I think you should add some different code to your ~/.bash_profile than what gelonida is suggesting.

This post used to contain valuable additional information that was removed by the mods for reasons I do not understand.

Bingle answered 12/4, 2020 at 16:31 Comment(0)
L
4

Recently, this only works on macOS:

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
fi

Previously, I used pyenv init - only, which stopped doing its job at some point. But, if you want autocompletion just append both of them together, like this:

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
  eval "$(pyenv init -)"
fi
Lindquist answered 18/10, 2021 at 10:31 Comment(2)
.bash_profile or .zprofile or both need to be modified for this to workAdrienneadrift
This fixes it for me on Monterey. It shouldn't be this hard to update python version!? 1 hour wastedAcerbic
A
2

You can try running pyenv-installer made by the same owners of pyenv. Running the script is as easy as

curl https://pyenv.run | bash
Anoxia answered 11/1, 2021 at 7:30 Comment(0)
F
0

in MACOS, change the version in ~/.python-version file to the installed version in the pyenv python version which one want to make global

Fronton answered 13/7, 2023 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.