Cannot switch Python with pyenv

后端 未结 6 891
忘掉有多难
忘掉有多难 2021-01-30 08:36

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         


        
相关标签:
6条回答
  • 2021-01-30 09:07

    After installing the correct version, close all terminals and try pyenv global 3.7.2 then try python --version, it should work

    0 讨论(0)
  • 2021-01-30 09:12

    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
    
    0 讨论(0)
  • 2021-01-30 09:22

    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

    0 讨论(0)
  • 2021-01-30 09:22

    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.

    0 讨论(0)
  • 2021-01-30 09:23

    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.

    0 讨论(0)
  • 2021-01-30 09:25

    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+)

    0 讨论(0)
提交回复
热议问题