Dave
Dave

Reputation: 1211

pyenv-virtualenvwrapper not working: wont use pyenv shell or pyenv local settings

I am unable to get pyenv-virtualenvwrapper plugin to work on my Ubuntu host. I have pyenv, virtualenvwrapper, and pyenv-virtualenvwrapper plugin installed (and in correct plugin directory).

  1. I tried running pyenv local 3.9.17, then python --version shows 3.9.17
  2. I then try to run mkvirtualenv project and then python --version shows wrong version

I also tried pyenv shell 3.9.17 before creating the env, same results. Even pyenv global 3.9.17 wont work in my vitual env. Note: I have been source ~/.bashrc every time I make a change in it. The github for pyenv-virtualenvwrapper says this:

Using pyenv virtualenvwrapper To setup a virtualenvwrapper into your shell, just run pyenv virtualenvwrapper. For example,

$ pyenv virtualenvwrapper or, if you favor virtualenvwrapper_lazy.sh,

$ pyenv virtualenvwrapper_lazy

Tried that, same results. Here is the bottom of my ~/.bashrc

# Python3 virtualenvwrapper
#export WORKON_HOME=$HOME/.virtualenvs
#export PROJECT_HOME=$HOME/projects
#source /usr/local/bin/virtualenvwrapper.sh
#source $HOME/.local/bin/virtualenvwrapper.sh
#export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python

#pyenv
#export PATH="$PATH:$HOME/.pyenv/libexec"
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

#pyenv-virtualenvwrapper plugin
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"
#pyenv virtualenvwrapper_lazy
pyenv virtualenvwrapper

# Rust stuff
. "$HOME/.cargo/env"
export PATH="$HOME/.cargo/bin:$PATH"

Why wont this work? Ideally I want to create the .python-version file in my repo, and then any time I workon project then it will use the correct python version.

Upvotes: 0

Views: 365

Answers (1)

Dave
Dave

Reputation: 1211

Ok I figured out how to get this to work. I found this github markdown file that details how to properly set this up. No where else were these instructions clear to me. Here they are below, I adjusted a few things, naming conventions, and set up env configurations in the ~/.bashrc instead of ~/.bash_profile.

  1. Completely remove pyenv, virtualenvwrapper. Update your ~/.bashrc to remove any settings referencing these.
sudo apt-get remove pyenv
rm -rf ~/.pyenv
sudo apt-get remove virtualenvwrapper
  1. Install [pyenv][] using [pyenv-installer][]:

    curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
    
  2. Activate pyenv for Terminal:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    
  3. Exit and restart Terminal.

  4. Install Python:

    pyenv install <some version>
    pyenv global <some version> system
    
  5. Exit and restart Terminal.

  6. Verify that you are now using the version of Python you just installed:

    python --version
    
  7. Install [pyenv-virtualenvwrapper][]:

    git clone https://github.com/yyuu/pyenv-virtualenvwrapper.git ~/.pyenv/plugins/pyenv-virtualenvwrapper
    
  8. Activate pyenv-virtualenvwrapper for Terminal:

    echo 'export WORKON_HOME="$HOME/.virtualenvs"' >> ~/.bashrc
    echo 'export PIP_VIRTUALENV_BASE="$WORKON_HOME"' >> ~/.bashrc
    echo 'pyenv virtualenvwrapper_lazy' >> ~/.bashrc
    
  9. Exit and restart Terminal.

  10. Now create the virtualenvwrapper using pyenv:

    mkdir $HOME/YOURGITHUBDIRECTORYORFOLDERLOCATION
    mkvirtualenv somename -a $HOME/YOURGITHUBDIRECTORYORFOLDERLOCATION
    

Now when I do workon it shows the correct version and the connects me to the specific directory (git repo directory) that the project is in. Very useful!

Not sure if pyenv global <some version> system was needed. I was able to set this back to the original python version the system was using and it did not affect the pyenv-virtualenvwrapper env I set up.

NOTE: If you want to change pyenv versions for an active project, you cant, you have to create a new one with the specific version you want.

Upvotes: 0

Related Questions