Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
12 tasks done
Forgo7ten opened this issue Mar 25, 2024 · 2 comments · Fixed by #478
Closed
12 tasks done

Comments

@Forgo7ten
Copy link

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

Prerequisite

  • Make sure your problem is not listed in the common build problems.
  • Make sure no duplicated issue has already been reported in the pyenv-virtualenv issues. You should look in closed issues, too.
  • Make sure you are not asking us to help solving your specific issue.
    • GitHub issues is opened mainly for development purposes. If you want to ask someone to help solving your problem, go to some community site like Gitter, StackOverflow, etc.
  • Make sure your problem is not derived from packaging (e.g. Homebrew).
    • Please refer to the package documentation for the installation issues, etc.
  • Make sure your problem is not derived from other plugins.
    • This repository is maintaining the pyenv-virtualenv plugin only. Please refrain from reporting issues of other plugins here.

Description

  • Platform information (e.g. Ubuntu Linux 20.04): ubuntu-22.04.4-live-server-arm64 and macOS 13.5 m1
  • OS architecture (e.g. amd64): arm64
  • pyenv version: pyenv 2.3.36
  • pyenv-virtualenv version: pyenv-virtualenv 1.2.2 (python -m venv)
  • Python version: 3.8.10
  • virtualenv version (if installed): no
  • Please attach the debug log of a faulty Pyenv invocation as a gist

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
Copy link
Author

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
Copy link
Author

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.

@Forgo7ten Forgo7ten changed the title Unable to use venv/bin/activate when the virtual environment of pyenv-virtualenv is set to global. Unable to use venv/bin/activate when the virtual environment of pyenv-virtualenv is set. Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant