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

recreate or abort if Python virtual environment has been moved from where it was originally located #12171

Closed
wants to merge 3 commits into from

Conversation

michaelDCurran
Copy link
Member

@michaelDCurran michaelDCurran commented Mar 14, 2021

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:

  • Executed runnvda.bat from a non-moved location, confirming that ensureVenv runs without errors and NVDA starts.
  • Renamed the NVDA repository directory, and performed the above step again. This time confirmed that the script asked if the virtual environment should be recreated. Saying no aborted. Running again and saying yes recreated the environment and then ran NVDA.

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.

  • Pull Request description is up to date.
  • Unit tests.
  • System (end to end) tests.
  • Manual tests.
  • User Documentation.
  • Change log entry.
  • Context sensitive help for GUI changes.

…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.
@michaelDCurran
Copy link
Member Author

@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.
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

environement -> environment

@@ -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 "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here.

"call", "echo", "%VIRTUAL_ENV%",
],
shell=True,
).decode('utf8').rstrip()
Copy link
Contributor

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

@AppVeyorBot
Copy link

See test results for failed build of commit 865ccce493

@AppVeyorBot
Copy link

See test results for failed build of commit 24b37dc980

Copy link
Contributor

@feerrenrut feerrenrut left a 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()
Copy link
Contributor

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.

@CyrilleB79
Copy link
Collaborator

Hi
I have tested this branch and confirm that the location change has been detected and I am asked if I want to re-generate the environment. Thus the error I get is clearer and it allows me to understand the issue; also the dev doc gives information for this. Thanks.

Unfortunately for me, it does not solve the situation that led me to open #12166.
Before #12075, it was possible to run NVDA from source on a computer that does not have Visual Studio installed: for this, I had to compile NVDA source code on PC1 (with Visual studio installed), copy the source code tree to PC2 (without Visual Studio installed) and run NVDA from source on PC2.
Now this does not seem to be possible anymore.
If this impossibility is unfortunately confirmed and that nothing can be done for this, please at least indicate this new limitation in the known issues of this PR since this only partly solves #12166.

@lukaszgo1
Copy link
Contributor

Unfortunately for me, it does not solve the situation that led me to open #12166.
Before #12075, it was possible to run NVDA from source on a computer that does not have Visual Studio installed: for this, I had to compile NVDA source code on PC1 (with Visual studio installed), copy the source code tree to PC2 (without Visual Studio installed) and run NVDA from source on PC2.
Now this does not seem to be possible anymore.
If this impossibility is unfortunately confirmed and that nothing can be done for this, please at least indicate this new limitation in the known issues of this PR since this only partly solves #12166.

Why it is impossible i.e what happens if you try?

@michaelDCurran
Copy link
Member Author

michaelDCurran commented Mar 15, 2021 via email

@CyrilleB79
Copy link
Collaborator

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.
This would already be something good to implement.

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.
That was because Python was not installed at the same place on the second computer.

After having manually modified Python location in pyvenv.cfg, I have finally succeeded in executing runnvda.bat on the second computer.
If something can be done to find the Python 3.8-32 location and update the pyvenv.cfg file, this would be very nice.

If not, modifying the activate.bat file is already a good thing.

Thanks!

@AppVeyorBot
Copy link

See test results for failed build of commit c67e4b2a8a

@michaelDCurran
Copy link
Member Author

Closing in favor of #12184

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 this pull request may close these issues.

Last NVDA alpha - Unable to rename folder of source code
5 participants