I use both direnv
and pyenv
and would like to add something to my .envrc
so that whenever I change directory, I also activate my virtual environment with pyenv.
I printed environment variables both when my virtualenv is active and also when it is not. There were a few pyenv variables set, which I added to my .envrc
(see below). I was hoping these would activate my pyenv virtual environment upon changing to the directory, but it didn't work.
I'll keep poking at this and trying to sort it out. If I find the answer, I'll update the question with the answer. In the meantime, I'm curious if anyone else has configured direnv
so that a virtual environment is loaded upon cd
'ing to a directory. If so, would you mind sharing how you did it?
** DID NOT WORK WHEN ADDED TO .envrc**
PYENV_VERSION=ds
PYENV_ACTIVATE_SHELL=1
PYENV_VIRTUAL_ENV=/Users/evan/.pyenv/versions/3.10.4/envs/ds
VIRTUAL_ENV=/Users/evan/.pyenv/versions/3.10.4/envs/ds
This can be done with pyenv-virtualenv rather than direnv. First, make
sure that eval "$(pyenv virtualenv-init -)"
is run in ~/.bashrc
. Then, to use
a named virtualenv in a particular directory, put the virtualenv name in a file
named .python-version
in that directory. For example in the directory ~/py
:
$ pyenv virtualenv my-venv
$ mkdir ~/py
$ echo my-venv >~/py/.python-version
$ cd ~/py
(my-venv) $ python -V
Python X.Y.Z