How can I make a virtual environment work with pyenv?
Asked Answered
D

3

11

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?

Dolan answered 10/2, 2019 at 23:25 Comment(1)
You need to have a virtual environment set up before one can be activated. What do you see when you execute pyenv virtualenvs?Filler
B
15

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 -)"
Benisch answered 11/2, 2019 at 13:13 Comment(6)
How do I add something to .bashrc. What is the shell on mac?Dolan
†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?Dolan
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.Benisch
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.6Dolan
I ALSO had to add eval "$(pyenv init --path)" to get this working. So 3 .zshrc additions.Dutiful
@Dutiful https://mcmap.net/q/1015668/-warning-pyenv-init-no-longer-sets-path-macosBenisch
G
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.

Get answered 16/10, 2019 at 1:33 Comment(0)
F
1

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.

Filler answered 11/2, 2019 at 0:55 Comment(2)
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-packagesDolan
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?Filler

© 2022 - 2024 — McMap. All rights reserved.