Cannot switch Python with pyenv

pyenv-virtualenv
pyenv install pip
pyenv global not working
pyenv-virtualenvwrapper
pyenv rebuild python
pyenv rehash
python switch version
switch between python versions

I would like to use pyenv to switch python2 and python3.

I successfully downloaded python2 and python3 and pyenv with following codes.

brew install pyenv

brew install pyenv-virtualenv

pyenv install 2.7.10

pyenv install 3.5.0

However, I cannot switch from python2 to python3..

Soma-Suzuki:~ Soma$ python --version
Python 2.7.10
Soma-Suzuki:~ Soma$ pyenv global
2.7.10
Soma-Suzuki:~ Soma$ pyenv versions
  system
* 2.7.10 (set by /Users/Soma/.pyenv/version)
  3.5.0
Soma-Suzuki:~ Soma$ pyenv global 3.5.0
Soma-Suzuki:~ Soma$ pyenv global
3.5.0
Soma-Suzuki:~ Soma$ pyenv versions
  system
  2.7.10
* 3.5.0 (set by /Users/Soma/.pyenv/version)
Soma-Suzuki:~ Soma$ python --version
Python 2.7.10
Soma-Suzuki:~ Soma$ 

I do not understand why this happens.

For your information. My python is in this directory.

Soma-Suzuki:~ Soma$ which python
/usr/bin/python

Thank you in advance.

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

pyenv doesn't switch python version � Issue #896 � pyenv/pyenv , pyenv doesn't switch python version #896. Closed. xareelee opened this issue on Apr 22, 2017 � 5 comments. Closed� You cannot change the interpreter after MATLAB loads Python. To change the interpreter, restart MATLAB, and then call pyenv. pe = pyenv ('Version',executable) specifies the full path to the Python executable. You can use this syntax on any platform or for repackaged CPython implementation downloads.

You forgot to add this eval "$(pyenv init -)" to the 1st line in .bash_profile file(if you're using a Mac) or .bashrc file.

pyenv global x.x.x not changing the python version � Issue #849 , and so far i've had no luck getting pyenv to switch python versions. pyenv will appear to be working properly, but the python binary won't� You’ve seen how pyenv can help you: Install multiple versions of Python Switch between the installed versions Use virtual environments with pyenv Activate different Python versions and virtual environments automatically

This answer is only for people that are using Fish shell and find this thread. Pyenv uses shims, ref, so in order to make pyenv work with your fish shell you have to edit your ~/.config/fish/config.fish file an append the pyen shim directory at the beginning of your $PATH variable. Here is what my config.fish looks like.

### PATH ###
set default_path /usr/local/bin /usr/bin /usr/sbin /bin /sbin
set macports /opt/local/bin
set androiddev ~/Android\ Development/platform-tools/
set rbenv ~/.rbenv/shims/
set pyenv ~/.pyenv/shims/
set anaconda /Users/m4punk/anaconda/bin/
set pg_config /Applications/Postgres.app/Contents/Versions/9.5/bin/


### Virtual Enviroment Wrapper ###

set -g VIRTUALFISH_HOME ~/Documents/Coding/python/virtualenvs
set -g VIRTUALFISH_DEFAULT_PYTHON /usr/local/bin/python3
eval (python -m virtualfish)

### NVM Settings ###
set -g NVM_DIR ~/.nvm

set -gx PATH $pyenv $default_path $macports $androiddev $rbenv $pg_config

setenv EDITOR sublime

The relevant lines here are

set pyenv ~/.pyenv/shims/

and

set -gx PATH $pyenv $default_path $macports $androiddev $rbenv $pg_config

The first creates a variable for the pyenv shim path, the second adds it to the front of your path variable. Just save&close, restarted your terminal session and you should be all set.

pyenv don't switch between python versions on macos � Issue #1446 , zshenv , but pyenv was still unable to switch between python versions. I think that maybe something about shell configuration (in my case zsh ),� You must ensure the shell will find the version of Python run by pyenv, not the one installed by default (which is often called the system version). If you don't change the path, here is the result:

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.

Pyenv doesn't change python version when activating / deactivating , pyenv/versions/3.8.1/envs/test doesn't activate deactivate the virtualenv and I don 't see (test) username:~ in my shell. pyenv for Windows. pyenv is a simple python version management tool. It lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. - pyenv-win/pyenv-win

I ran into the same problem and ended up making some changes to the way pyenv init goes about setting up the shell but in the end it works the same. The only real difference from the guide on the pyenv github page is that I had to add the $(pyenv root)/bin directory to my path too.

The reason I did this was to avoid long shell startup times from running eval "$(pyenv init -)" and all the other .bash_profile work that goes into my local shell environment. Just to be clear; Pyenv itself doesn't create a bad shell experience, in my humble opinion, but when you work with several languages that all have their own version management systems and tools that like to be initialized from the .profile with pyenv, etc., the shell initialization process can get slow.

Here's the steps I took to set myself up, at a high view:

  1. Run the dry-run version of the pyenv init command so you can see what it would have done for you.
  2. Put the PATH and shell environment vars into your .bash_profile (or whatever file your distro uses).
  3. Put the function pyenv init printed into your .bashrc and source your .bashrc from your .bash_profile

This is one way to get it done but it's better to use this as "pseudo-code". You should exchange .bash_profile for whatever file your distro prefers.

$ pyenv init - # use the output for reference, it doesn't actually do anything
$ cat <<EOBP > ~/.bash_profile
export PYENV_SHELL=bash
PATH=$(pyenv root)/shims:$(pyenv root)/bin:$PATH
[ -f  /usr/local/Cellar/pyenv/1.2.9/completions/pyenv.bash ] && . /usr/local/Cellar/pyenv/1.2.9/completions/pyenv.bash
[ -f ~/.bashrc ] && . ~/.bashrc
EOBP

The next bit updates your shell with a new bit of logic that we copied from the pyenv init dry run from step 1, above.

$ cat <<EORC > ~/.bashrc
# from $(pyenv init -)
pyenv() {
    local command
    command="${1:-}"
    if [ "$#" -gt 0 ]; then
        shift
    fi

    case "$command" in
    rehash|shell)
        eval "$(pyenv "sh-$command" "$@")";;
    *)
        command pyenv "$command" "$@";;
    esac
}
EORC

Trouble Switching Python Versions � Issue #185 � pyenv/pyenv , I'm finding that pyenv rarely switches installed versions of python. Somehow, I got pyenv to switch from the system python to 2.7.7, but I can't� To disable pyenv managing your Python versions, simply remove the pyenv init line from your shell startup configuration. This will remove pyenv shims directory from PATH, and future invocations like python will execute the system Python version, as before pyenv.

pyenv can not change the python version � Issue #1492 � pyenv , Description Platform information : macOS 10.15.2 pyenv version: 1.2.9 Python version: 3.7.2 re35 ➤ pyenv versions system * 3.5.3 (set by� After using Python: Select Interpreter, that interpreter is applied when right-clicking a file and selecting Python: Run Python File in Terminal. The environment is also activated automatically when you use the Terminal: Create New Integrated Terminal command unless you change the python.terminal.activateEnvironment setting to false .

Managing Multiple Python Versions With pyenv – Real Python, Luckily, managing multiple version of Python doesn't have to be confusing if you use pyenv . How would you switch quickly between the different versions? The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.

pyenv Tutorial - Guides - Resources, Previously known as Pythonbrew, pyenv lets you change the global Python If you can't function without your virtual environments anymore then fear not,�

Comments
  • You can just edit your .bashrc file. Make alias python=Python3.5.0
  • Per the documentation given, place eval "$(pyenv init -)" toward the end of .bash_profile. Note that for me, placing it in .bashrc did not work. (The docs warn about "strange" behavior if .bashrc is used.)
  • Running eval "$(pyenv init -)" doesn't change the python -V in my case, even though I do have the pyenv versions installed in ~/.pyenv
  • I was missing export PATH="$PYENV_ROOT/shims:$PATH" in .zshrc in addition of the export PATH="$PYENV_ROOT/bin:$PATH" I had configured
  • and finally don't forget to restart your shell so the path changes take effect $ exec "$SHELL"
  • per the documentation it should be towards the end of the file
  • @Jonathan no different