Issues with pyenv-virtualenv: Python and PIP not changed when activating / deactivating virtual environment
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.
I also had similar problem. The solution was to change the entries I put in my ~/.bashrc
. I went on to export the variable export PYENV_ROOT="$HOME/.pyenv"
and added the line eval "$(pyenv init --path)"
.
Run the command below in full and it will add the necessary entries to the ~/.bashrc
of the user you are using.
read -r -d '' FILE_CONTENT << 'HEREDOC'
BEGIN
# >>>>>>
# pyenv configurations.
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)" # This only sets up the path stuff.
eval "$(pyenv init -)" # This makes pyenv work in the shell.
eval "$(pyenv virtualenv-init -)" # Enabling virtualenv so it works natively.
# <<<<<<
END
HEREDOC
echo -n "${FILE_CONTENT:6:-3}" | tee -a ~/.bashrc
NOTE: Tested on Manjaro (Linux, Arch based).
Thanks! =D
[Ref(s).: https://github.com/pyenv/pyenv-installer , https://github.com/pyenv/pyenv , https://realpython.com/intro-to-pyenv/ , https://github.com/pyenv/pyenv-virtualenv/issues/390#issuecomment-852599456 , https://www.giters.com/pyenv/pyenv-virtualenv/issues/407 ]