Snhorq
Snhorq

Reputation: 133

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?

Upvotes: 13

Views: 20631

Answers (3)

Chris Larson
Chris Larson

Reputation: 1724

You'll need to actually create my-virtualenv using either pyenv-virtualenv, or one of the other virtual environment tools available, before you can activate it. Given that you cite pyenv-virtualenv in your question, here's an example:

pyenv virtualenv 3.6.5 my-virtualenv-3.6.5

This creates a virtual environment named my-virtualenv-3.6.5 containing Python 3.6.5.

Of course, you can name your environment whatever you'd like (my-virtualenv is fine), but it's never a bad idea to name things for your future self, because that person won't necessarily remember what it was for. You might consider QGIS-virtualenv-3.6.5, in fact, for this particular application.

pyenv virtualenv 3.6.5 QGIS-virtualenv-3.6.5

Once you've got a virtual environment, then go ahead and do:

pyenv activate QGIS-virtualenv-3.6.5

(Or whatever you choose as your virtualenv name.

Upvotes: 2

Luis
Luis

Reputation: 31

Try this: into the terminal,

  1. write: nano ~/.bashrc

  2. add in the end:

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  1. Exit and save
  2. into the terminal write: source ~/.bashrc

And it's all, this worked for me.

Upvotes: 3

phd
phd

Reputation: 94827

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

Upvotes: 18

Related Questions