10

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?

Snhorq
  • 103
  • 1
  • 1
  • 7
  • You need to have a virtual environment set up before one can be activated. What do you see when you execute `pyenv virtualenvs`? – Chris Larson Feb 11 '19 at 00:41

3 Answers3

14

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 -)"
phd
  • 82,685
  • 13
  • 120
  • 165
  • How do I add something to `.bashrc`. What is the shell on mac? – Snhorq Feb 11 '19 at 15:55
  • †he QGIS software comes in a .pkg. Now that I'm in my virtual environment, `(my-virtualenv) bash-3.2$`, how do I open the package just in the environment? – Snhorq Feb 11 '19 at 16:01
  • *How do I add something to .bashrc?* `vim ~/.bashrc`. Or `echo 'eval "$(pyenv init -)"' > ~/.bashrc` *What is the shell on mac?* I see `bash-3.2` in your question, so I'm sure the shell is `bash`. *QGIS software comes in a .pkg. … how do I open the package just in the environment?* No idea what is that **pkg** but it's certainly not a Python thing. – phd Feb 11 '19 at 16:24
  • The pkg is a software called QGIS. Can I install software just to the virtual environment? I set up the environment because the software requires python 3.6 – Snhorq Feb 11 '19 at 16:40
  • I ALSO had to add `eval "$(pyenv init --path)"` to get this working. So 3 `.zshrc` additions. – potench Jul 14 '21 at 00:11
  • @potench https://stackoverflow.com/a/68278214/7976758 – phd Jul 14 '21 at 08:20
3

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.

Olympiloutre
  • 2,268
  • 3
  • 28
  • 38
Luis
  • 31
  • 1
0

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 considerQGIS-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.

Chris Larson
  • 1,684
  • 1
  • 11
  • 19
  • That gets me Requirement already satisfied: setuptools in /Users/user/.pyenv/versions/3.6.5/envs/QGIS-virtualenv-3.6.5/lib/python3.6/site-packages Requirement already satisfied: pip in /Users/user/.pyenv/versions/3.6.5/envs/QGIS-virtualenv-3.6.5/lib/python3.6/site-packages – Snhorq Feb 11 '19 at 15:53
  • I think perhaps we may be missing a step in your actions here. You're telling me that executing `pyenv activate QGIS-virtualenv-3.6.5` immediately gives you `Requirement already satisfied` without you taking any other action after typing that? Is that what you mean to say? – Chris Larson Feb 11 '19 at 16:24