pyenv / pyenv-virtualenv

a pyenv plugin to manage virtualenv (a.k.a. python-virtualenv)
MIT License
6.18k stars 399 forks source link

Unable to use `venv/bin/activate` when the virtual environment of pyenv-virtualenv is set. #477

Closed Forgo7ten closed 3 months ago

Forgo7ten commented 3 months ago

Too many issues will kill our team's development velocity, drastically. Make sure you have checked all steps below.

Prerequisite

Description

Additional

Execute the following command to create a virtual environment and set the virtual environment as global.

pyenv virtualenv 3.8.10 py38
pyenv global py38
python -m venv venv

Then I cannot activate the customized environment through the following command.

source venv/bin/activate

I determined that this was an issue with pyenv-virtualenv, and it was reproducible reliably on both macOS and my newly created ubuntu server (for privacy reasons).

Forgo7ten commented 3 months ago

To add, this happens not only when the virtual environment is set to global. As long as the virtual environment (including local) is set, it cannot be set again. I can understand that this feature cannot be set again after local setting. But I am used to setting a virtual environment globally (clean) instead of a specified version, which results in me being unable to activate the virtual environment in a certain folder again. ... I tested the pyenv local and pyenv global settings respectively. For a specific version, it will not affect the use of my venv; but if a virtual environment is activated, I cannot activate a local environment. After executing the ./venv/bin/activate script, I checked the PATH environment variable and found that there was no path to the current venv. This happens only when pyenv-virtualenv is activated.

Forgo7ten commented 3 months ago

I found this was caused by PROMPT_COMMAND or precmd_functions registered by the pyenv-virtualenv-init script. I can fix this by removing eval "$(pyenv virtualenv-init -)" in bashrc, but this leaves me without a virtualenv prompt.

By inserting the following command on line 133 of this script, you can solve the problem that the environment variable cannot be written.

    local ret=\$?
+   _OLD_VIRTUAL_PATH="\$PATH"
    if [ -n "\${VIRTUAL_ENV-}" ]; then

However, bash's PS1 environment variables, such as display (venv) may require more code modifications. I'm not very familiar with these, so I just added _OLD_VIRTUAL_PS1="\$PS1" in the same position as above. The effect of this will be a bit funny, it will become the prefix of (py38) (venv) (py38), even after executing the venv/bin/activate script multiple times, it will become (py38) (venv ) (venv) (venv) (py38). But at least it works.

I don't have much energy to get familiar with the code, so let it be. I still hope you can fix this problem, thank you again.