Cannot switch Python with pyenv

后端 未结 6 907
忘掉有多难
忘掉有多难 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: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 < ~/.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 < ~/.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
    

提交回复
热议问题