-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
venv activate script would be good to show failure. #88452
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
Comments
I changed the path of the project using venv, so it didn't work properly. I thought it worked successfully because there was a (venv) on the terminal line. However, the __VENV_DIR__ in the set "VIRTUAL_ENV=VENV_DIR" in the activate script did not work properly because it was the PATH before the replacement. How about adding a procedure to verify the __VENV_DIR__ is a valid PATH to the Activate Script? |
venvs aren't meant to be portable (i.e. renaming the directory or moving to a new location). Scripts installed into a venv have absolute paths pointing to the location when the venv was created. venvs should be treated as throwaway resources that can be readily recreated - so if you need a venv in a new location, just create it there and install the desired packages again. |
Thank you for your explanation of venv. I understand that venv is not portable. But I was confused because the venv was written on the left side of the terminal line and it looked like it was working. Therefore, if venv does not work, such as if __venv_dir__ is invalid path, i thought any warning message or exception handling was necessary. Or (venv) doesn't show up. |
No, because these are just scripts created by venv and placed in the venv's directory tree. If you then move the files to a different location, they aren't engineered to notice, and so will not behave as you expect. As venv's aren't meant to be moved, there's no particular reason to engineer for that possibility. OK to close this issue as "not a bug"? |
Okay. Thank you for the detailed explanation. |
I suggest re-opening this issue. These problems seem common and elusive[1] and the fix is seemingly easy to implement. MotivationIf an unknowing user...
...then, the current behaviour is:
(this happened for me; I came here after hours of trouble-shooting) The desired behaviour would be:
Apparently not, which is fine. This would be an argument for this easy fix. Renaming or moving a source/project dir is common, and some users will expect it to be possible, believing paths are relative, as is most common for tools working on source dirs. If in doubt, it's not unlikely for a user to simply try, and then believe all is well since the venv seemingly activates successfully (the prompt changes). Often, no problems show up at first; instead subtle problems appear later, stemming from unknowingly installing to and using global packages. FixIt seems this fix would be easy to implement. https://github.com/python/cpython/blob/main/Lib/venv/scripts/common/activate: now: [...]
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV="__VENV_DIR__"
export VIRTUAL_ENV
[...] change to something like: [...]
# unset irrelevant variables
deactivate nondestructive
if [ ! -d "$__VENV_DIR__" ]; then
echo "Error: virtual environment directory $__VENV_DIR__ doesn't exist." >&2
else
VIRTUAL_ENV="__VENV_DIR__"
export VIRTUAL_ENV
[...] # (rest of file in this else-clause)
fi (The absolute path in [1]: seemingly common and frustrating problemSome examples of reports of this specific problem below. Most mention being misled by lack of error message and by believing the prompt changing indicated successful activation. There's sometimes a high degree of frustration, and a simple error message seemingly would have helped most of them. Several at first had no indication that the experienced problems were caused by a move/rename of the source dir.
(there are many more) Some SO question on moving source dirs with venv, and on whether this is possible/problems that follow from it:
NotesFor reference, here are some requests to make the venv paths relative, along with explanations for why that would be a lot of work/not viable: |
…ythonGH-98155) (cherry picked from commit 6a757da) Co-authored-by: Vinay Sajip <[email protected]>
…ythonGH-98155) (cherry picked from commit 6a757da) Co-authored-by: Vinay Sajip <[email protected]>
I added a warning about non-portability of venvs to the documentation. I can try and review any PRs with tests that are related to error checking in the activation scripts. |
Doc update: that's great. Activation script error check: I'm not the right person to make a PR unfortunately. I still suggest re-opening this issue so an error check is addd eventually. Not having one has evidently caused a lot of users a lot of trouble and the fix is straight-forward. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: