Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot switch Python with pyenv

Tags:

python

pyenv

People also ask

Can I use pip with Pyenv?

When using pyenv , you should be able to set your 'local' version in the directory you are working in, and then pip will rely on this version.


[July 2021]
If you see this message when running eval "$(pyenv init -)"

WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.

you should check the message from pyenv init as the warning says, but in a nutshell, you can use eval "$(pyenv init --path)" instead.

And don't forget to accordingly update your ~/.bash_profile, ~/.zprofile, ~/.bashrc, ~/.zshrc or the like if necessary.


Try this: eval "$(pyenv init -)"

Example:

$ python -V
Python 2.7.9
mac:~ $ eval "$(pyenv init -)"
mac:~ $ python -V
Python 3.5.0

More info: https://github.com/pyenv/pyenv


For me, this worked on MacOS with ZSH after installing via Homebrew:

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

I initially had eval "$(pyenv init -)" inside of my ~/.zshrc file, but that didn't seem to work. After following this guide: https://github.com/pyenv/pyenv#basic-github-checkout I remved the eval call from the .zshrc file and added it to the .zprofile file using the above command and after restarting terminal everything worked.


You forgot to add this eval "$(pyenv init -)".

Add this to your .bash_profile or .bashrc file (mac <=10.14) or to your .zshrc file (mac 10.15+)


In ubuntu the ~/ .bashrc file needs to be updated and change eval "$(pyenv init -)" to eval "$(pyenv init --path)"


This is a great opportunity to learn about how pyenv works under the hood.

The pyenv global command simply reads the data in your /Users/Soma/.pyenv/version directory. It's basically the same as cat /Users/Soma/.pyenv/version.

The pyenv versions command is just checking through the hierarchy and selecting the right Python version to use when a "shim interceptable" command like python or pip is run.

When you run pyenv global 3.5.0, the /Users/Soma/.pyenv/version file is updated to contain "3.5.0". That's the only change pyenv makes. Most users are surprised that pyenv global 3.5.0 only changes a single line in a text file!

When you run python --version, your Terminal will perform the same steps it performs when any shell command is executed: it goes through each directory in your PATH and looks for the first executable named python.

If you type echo $PATH, you'll have something like this: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Your machine is finding the python executable in the /usr/bin directory.

You can add this code to your ~/.bash_profile file to change your PATH.

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

Restart your terminal, run echo $PATH again, and you'll now see output like this: /Users/Soma/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Notice how the /Users/Soma/.pyenv/shims directory is at the start of the PATH now. When you run python --version now, the command will be handled by the python executable in /Users/Soma/.pyenv/shims. The command won't have an opportunity to be picked up by /usr/bin/python because it'll be grabbed by /Users/Soma/.pyenv/shims/python first.

I can see why this bug confuses you. It's hard to debug this unless you know how pyenv works.


Background

  • now: 20210926
  • Mac: 10.15.7
  • pyenv: 2.0.7

Solution

for current console
eval "$(pyenv init --path)"
for work after every boot

if you want auto take effect after Mac boot, then add it to you boot script

here my is zsh, so:

vi ~/.zshrc

add

eval "$(pyenv init --path)"

done.