3

I'm a fan of virtual environments, and I also like to keep my virtualenvs in the root project directory. This helps me keep track of where they are and allows me to call them generic names, like .venv.

But I need a way to install specific versions of python for use with my virtualenvs. All roads seem to point to pyenv.

Unless I'm misunderstanding how pyenv and pyenv-virtualenv work, this libary seems to insist on burying my actual virtualenv somewhere in the ~/.pyenv folder, and leaving me with a .python-version file, which I'd rather not have.

Isn't there a way to install specific versions of python with the ease of use of pyenv, but then create my virtual environments in the traditional way, inside my project directory, thus removing my projects's dependency on pyenv after the virtual environment has been created?

John Kealy
  • 1,503
  • 1
  • 13
  • 32

1 Answers1

10
# you need install the target version first
pyenv install 3.9.5
# then sets a shell-specific Python version
pyenv shell 3.9.5
# use this python version to create virtualenv
python -m venv .venv
# or use virtualenv
virtualenv .venv -p $(pyenv which python)

HALF9000
  • 518
  • 2
  • 14
  • Exactly what I was looking for, thank you kind sir! – John Kealy Sep 06 '21 at 14:58
  • 1
    I wish the docs made this as clear! One thing to note for anyone else using this, if you are changing the global python and you have any projects in the system that precede pyenv's installation, be sure to set the global back to whichever python you were using before running those projects. – user3507825 Sep 04 '22 at 14:24