pythonpython-3.xvirtualenvpyenv

How can I make a virtual environment work with pyenv?


I'm trying to use QGIS, which requires python 3.6.x.

I'm on mac on a system that already has python 2.7 and 3.7.

I tried

brew update
brew install pyenv
brew install pyenv-virtualenv
pyenv install 3.6.5

It installs just fine. Then, when I try to activate

pyenv activate my-virtualenv

I get this error

Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly. Please restart current shell and try again.

I tried again with

exec $SHELL
pyenv activate my-virtualenv

And received the same error.

I executed this command in bash-3.2$ and regular terminal

if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

And I'm still getting the same error. How can I get an environment running that uses python 3.6?


Solution

  • Initialize pyenv:

    exec $SHELL
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    pyenv activate my-virtualenv
    

    To save yourself some typing add this to your .bashrc:

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