How can I make a post virtualenv hook in pyenv to upgrade pip packages

Viewed 473

I am using pyenv to manage different Python versions on my machine, and pyenv-virtualenv to manage my venvs. When I create a new venv with something like pyenv virtualenv foo it doesn't install the latest version of pip, setuptools, and wheel into the venv. I would like to run pip install --upgrade pip setuptools wheel inside each venv after it has been created.

pyenv has the concept of hooks for before and after a command. I am able to run this command by adding the following file

~/.pyenv/plugins/pyenv-virtualenv/etc/pyenv.d/virtualenv/after.bash

with the following contents

after_virtualenv 'pip install --upgrade pip setuptools wheel'

The command is run, but it is not run inside the venv, so it just uses the current pip. How can I call this command inside the venv in the after_virtualenv hook?

1 Answers

Okay I figured it out you can just specify just created virtualenv name as the PYENV_VERSION and call pyenv-exec.

upgrade_packages() {
  PYENV_VERSION=$VIRTUALENV_NAME pyenv-exec pip install --upgrade pip setuptools wheel
}

after_virtualenv 'upgrade_packages'
Related