How can I make a virtual environment work with pyenv?

10,472

Solution 1

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

Solution 2

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.

Share:
10,472
Snhorq
Author by

Snhorq

Updated on July 18, 2022

Comments

  • Snhorq
    Snhorq almost 2 years

    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
    Snhorq about 5 years
    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
    Snhorq about 5 years
    How do I add something to .bashrc. What is the shell on mac?
  • Snhorq
    Snhorq about 5 years
    †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?
  • phd
    phd about 5 years
    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.
  • Chris Larson
    Chris Larson about 5 years
    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?
  • Snhorq
    Snhorq about 5 years
    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
  • potench
    potench almost 3 years
    I ALSO had to add eval "$(pyenv init --path)" to get this working. So 3 .zshrc additions.
  • phd
    phd almost 3 years