AJ-
AJ-

Reputation: 1167

Set default python with pyenv

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

Upvotes: 40

Views: 56321

Answers (7)

gelonida
gelonida

Reputation: 5640

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 like

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

Upvotes: 20

sahasrara62
sahasrara62

Reputation: 11237

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

Upvotes: 0

Oded BD
Oded BD

Reputation: 3286

For me OSX I had to put

eval "$(pyenv init --path)"

inside my ~/.bashrc | ~/.zshrc

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

Upvotes: 57

Yar
Yar

Reputation: 7476

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

Upvotes: 16

shx
shx

Reputation: 1138

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

Upvotes: 4

Powers
Powers

Reputation: 19328

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.

Upvotes: 5

enchance
enchance

Reputation: 30501

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

Upvotes: 2

Related Questions