Issue
I installed pyenv-virtualenv
using Linuxbrew (Homebrew 2.2.5) on my Ubuntu 16.04 VPS. The pyenv version is: 1.2.16
. Now when I do a test like this:
pyenv install 3.8.1
pyenv virtualenv 3.8.1 test
cd /.pyenv/versions/3.8.1/envs/test
pyenv local 3.8.1
Then entering / leaving the /.pyenv/versions/3.8.1/envs/test
doesn't activate deactivate the virtual environment and I don't see (test) username:~
in my shell. I also created a /home/users/test
directory and .python-version
there but still entering / leaving directory does nothing.
Accordingn to the documentation:
If eval "$(pyenv virtualenv-init -)" is configured in your shell, pyenv-virtualenv will automatically activate/deactivate virtualenvs on entering/leaving directories which contain a .python-version file that contains the name of a valid virtual environment as shown in the output of pyenv virtualenvs (e.g., venv34 or 3.4.3/envs/venv34 in example above) . .python-version files are used by pyenv to denote local Python versions and can be created and deleted with the pyenv local command.
So first question is: Why this doesn't work? Why the virtual environment is not activated / deactivated automatically at entering / leaving a directory containing a .python-version
file?
Also when I activate virtualenv by hand pyenv activate test
and then check the Python version, it prints the system Python version and not the one from environment: Python 3.8.1
:
python --version
Python 3.7.6
I can get the right Python version only by directly referring to virtualenv shims Python like this:
which python
/home/andre/.pyenv/shims/python
/home/andre/.pyenv/shims/python --version
Python 3.8.1
The behaviour is the same whenever the virtualenv "test" is activated or not.
I would expect that after activating "test" the command python --version
returns Python 3.8.1
So second question: why pip
and python
are not switched when activating / deactivating the virtual environment ?
Are these pyenv bugs? Or am I doing something wrong?
Solution
It turns out that in order to automatically activate / deactivate a venv
when entering / leaving a directory the .python-version
file in there must contain the venv name
and not the Python version
associated with that venv
So executing:
pyenv local 3.8.1
creates a .python-version
file which only includes the Python version 3.8.1
.
Then entering / leaving a directory containing .python-version
file will set / unset the Python version specified in that file but will not activate / deactivate any venv
.
To create a .python-version
file that will do both: activate a virtual environment and set Python version the command should look like: pyenv local test
where test
is a venv created with:
pyenv virtualenv 3.8.1 test
.
So changing 3.8.1
to test
in the .python-version
fixed the problem.
After I done this the venv
was activated / deactivated when entering / leaving directory containing .python-version
.
But still the Python version did not change to the one associated with the venv
(in this case 3.8.1
)
Then I found out that I had two lines in my .profile
that was causing this problem:
alias python=/home/linuxbrew/.linuxbrew/bin/python3
alias pip=/home/linuxbrew/.linuxbrew/bin/pip3
After removing these lines everything works as expected.
Answered By - Tony Answer Checked By - Clifford M. (WPSolving Volunteer)