pyenv-virtualenv
pyenv-virtualenv copied to clipboard
auto load venv
Hello all,
I am trying the autoenable of venv part. I have the eval "$(pyenv virtualenv-init -)"
set in my shell rc file and also have the venv in the .python-version
file of the directory. But then I try to cd
to the dir, the prompt does not get set as expected.
⚡ ~ $ cat private_workspace/py_neo4j/.python-version
3.7.3
myneo4j
⚡ ~ $ cd private_workspace/py_neo4j/
⚡ ~/private_workspace/py_neo4j $ pyenv virtualenvs
3.7.3/envs/myneo4j (created from /Users/krishna/.pyenv/versions/3.7.3)
* myneo4j (created from /Users/krishna/.pyenv/versions/3.7.3)
When I manually run the activate
step I do get the prompt
⚡ ~/private_workspace/py_neo4j $ pyenv activate myneo4j
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(myneo4j) ⚡ ~/private_workspace/py_neo4j $
Basically the venv is active at this point and if I open a new terminal to the same dir, the prompt is not set and I am not sure if I am running in the venv
and I get the bellow message along with the expected pip packages installed as per the project.
⚡ ~/private_workspace/py_neo4j $ pyenv activate myneo4j
pyenv-virtualenv: version `myneo4j' is already activated
⚡ ~/private_workspace/py_neo4j $
The behaviour is some times not consistent like in the case of a new venv setup the shell prompt does not change and its confusing at times
⚡ ~/private_workspace/flamescope (master ⚒) $ python --version
Python 2.7.16
⚡ ~/private_workspace/flamescope (master ⚒) $ pyenv versions
system
2.7.16
2.7.16/envs/flamescope
3.7.3
3.7.3/envs/myneo4j
* flamescope (set by /Users/krishna/private_workspace/flamescope/.python-version)
myneo4j
⚡ ~/private_workspace/flamescope (master ⚒) $ pyenv activate flamescope
pyenv-virtualenv: version `flamescope' is already activated
⚡ ~/private_workspace/flamescope (master ⚒) $
I am using a mac os 10.14.3 and zsh (with oh-my-zsh plugin). Am I missing something elementary? Please advice.
Well, I have the same issue and then I wrote this plugin to solve that
https://github.com/alencarandre/pyenv-autoenv
It works fine with bash
. I ~dont~ didn't test it with zsh
.
Me: comes here not expecting a solution, and literally 5hrs earlier @alencarandre posts a solution ready for me to try out :-)
But of course it would be better if the advertised solution (as per the README) actually worked 🙂 so I'm going to hold out for that and just manually activate environments for now.
Just searched for it myself and it seems https://github.com/strokirk/pyenv-autoenv looks promising. The completion is a bit buggy, but at least it works.
cb@asterix:pts/1->/home/cb/tmp/autoenv/testfolder (0)
> pyenv autoenv
Using Python version 3.9.9
Downloading Python-3.9.9.tar.xz...
-> https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tar.xz
Installing Python-3.9.9...
Installed Python-3.9.9 to /home/cb/.pyenv/versions/3.9.9
Creating new virtualenv 'testfolder' with Python 3.9.9...
Looking in links: /tmp/tmppdz9eips
Requirement already satisfied: setuptools in /home/cb/.pyenv/versions/3.9.9/envs/testfolder/lib/python3.9/site-packages (58.1.0)
Requirement already satisfied: pip in /home/cb/.pyenv/versions/3.9.9/envs/testfolder/lib/python3.9/site-packages (21.2.4)
Setting local virtualenv 'testfolder'...
cb@asterix:pts/1->/home/cb/tmp/autoenv/testfolder (0)
> pyenv which python
/home/cb/.pyenv/versions/testfolder/bin/python
cb@asterix:pts/1->/home/cb/tmp/autoenv/testfolder (0)
> cd ..
cb@asterix:pts/1->/home/cb/tmp/autoenv (0)
> pyenv which python
/home/cb/.pyenv/versions/3.10.2/bin/python
cb@asterix:pts/1->/home/cb/tmp/autoenv (0)
> cd testfolder
cb@asterix:pts/1->/home/cb/tmp/autoenv/testfolder (0)
> pyenv which python
/home/cb/.pyenv/versions/testfolder/bin/python
cb@asterix:pts/1->/home/cb/tmp/autoenv/testfolder (0)
> cat .python-version -p
testfolder