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
recreate or abort if Python virtual environment has been moved from where it was originally located #12171
Conversation
…re they were created in their activation script, dedetect if the venv has been copied to another location and if so ask the user if the environment should be recreated at the new location or to abort.
@CyrilleB79 could you please test this branch and confirm this detects your situation and allows you to recreate the environment or abort? |
…ted if it has been moved from its original place of creation.
venvUtils/ensureVenv.py
Outdated
Verifies that the Python virtual environment at c{VENV_PATH} was actually created at that location, | ||
and not just copied or moved there. | ||
""" | ||
# Activate the Python virtual environement and capture the content of the VIRTUAL_ENV environment variable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
environement -> environment
venvUtils/ensureVenv.py
Outdated
@@ -101,6 +124,16 @@ def ensureVenvAndRequirements(): | |||
if not os.path.exists(venv_path): | |||
print("Virtual environment does not exist.") | |||
return createVenvAndPopulate() | |||
if not verifyExistingVenvPath(): | |||
if askYesNoQuestion( | |||
"A Python virtual environement cannot be activated from a different location " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here.
venvUtils/ensureVenv.py
Outdated
"call", "echo", "%VIRTUAL_ENV%", | ||
], | ||
shell=True, | ||
).decode('utf8').rstrip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows console isn't Unicode by default. It'll probably work fine as long as there is no characters outside of the ASCI range in the path but to be safe this should be decoded using oem
See test results for failed build of commit 865ccce493 |
See test results for failed build of commit 24b37dc980 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @michaelDCurran, this approach looks good.
existingPath = subprocess.check_output( | ||
[ | ||
os.path.join(venv_path, "scripts", "activate.bat"), | ||
"&&", | ||
"call", "echo", "%VIRTUAL_ENV%", | ||
], | ||
shell=True, | ||
).decode('oem').rstrip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you add a comment here to show an example of the output for this command? If the output changes, this will make it easier to verify.
Hi Unfortunately for me, it does not solve the situation that led me to open #12166. |
Why it is impossible i.e what happens if you try? |
We could consider either providing our own version of activate.bat (it
does not look that complicated) which uses relative paths. Or, we could
programmatically rewrite activate.bat after it is generated, changing
the absolute paths to relative paths.
I think I prefer the first option.
|
In activate.bat, I have tried to change manually the path stored in VIRTUAL_ENV variable. This should work if the folder is just moved/renamed on the same computer. However, when trying to copy the source tree to another computer and execute runnvda.bat, Python complained that it did not find the pythonw.exe. After having manually modified Python location in pyvenv.cfg, I have finally succeeded in executing runnvda.bat on the second computer. If not, modifying the activate.bat file is already a good thing. Thanks! |
See test results for failed build of commit c67e4b2a8a |
Closing in favor of #12184 |
Link to issue number:
Closes #12166
Summary of the issue:
When creating a Python virtual environment, Python's venv package hard-codes the absolute path where the environment was created, in its activation script. This therefore means that it is impossible to use a Python virtual environment if it has been copied or moved to a different location. This is a limitation in venv, and there really is nothing that NvDA can do to work around this.
However, NvDA should at least offer to recreate the virtual environment at the new location if it has been moved or copied.
As currently, executing runnvda.bat or other scripts while in this state cause hard to understand errors such as NVDA thinking that there is no virtual environment at all.
Description of how this pull request fixes the issue:
ensureVenv.py detects if the Python virtual environment has been moved by temporarily activating it, and capturing the content of the VIRTUAL_ENV environment variable, and then comparing that against the physical venv path.
If they differ, the user is asked if the virtual environment should be recreated. Saying no causes the script to abort.
Testing strategy:
Known issues with pull request:
None known.
Change log entry:
None needed.
Code Review Checklist:
This checklist is a reminder of things commonly forgotten in a new PR.
Authors, please do a self-review and confirm you have considered the following items.
Mark items you have considered by checking them.
You can do this when editing the Pull request description with an x:
[ ]
becomes[x]
.You can also check the checkboxes after the PR is created.