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

Python: fix virtualenv with Python 2. #88756

Merged
merged 2 commits into from May 24, 2020
Merged

Python: fix virtualenv with Python 2. #88756

merged 2 commits into from May 24, 2020

Conversation

FRidh
Copy link
Member

@FRidh FRidh commented May 24, 2020

Motivation for this change
Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

cc @calbrecht

@FRidh FRidh requested a review from jonringer as a code owner May 24, 2020 07:38
@FRidh FRidh requested a review from calbrecht May 24, 2020 07:38
@calbrecht calbrecht self-requested a review May 24, 2020 08:33
@FRidh FRidh merged commit f17001a into NixOS:staging May 24, 2020
@FRidh FRidh deleted the virtualenv branch May 24, 2020 08:43
Copy link
Member

@calbrecht calbrecht left a comment

Choose a reason for hiding this comment

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

Ay, i totally messed up this review thing. Did not use this feature so often on github and therefore forgot to submit.

But now that you gave a link to a pull request on cpython where pyvenv.cfg also was present, i thought it might be relevant.

Also you are very quick on merging @FRidh.
I am not that fast, especially on a subject i am really not accustomed to.

# For Python 3 we check whether our `base_prefix` is different from our current `prefix`.
# For Python 2 we check whether the non-standard `real_prefix` is set.
# https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
in_venv = (sys.version_info.major == 3 and sys.prefix != sys.base_prefix) or (sys.version_info.major == 2 and hasattr(sys, "real_prefix"))
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it is more aproriate to use pyvenv.cfg from module-site but i must admit i only have partial understanding of the matter.

If a file named “pyvenv.cfg” exists one directory above sys.executable, sys.prefix and sys.exec_prefix are set to that directory and it is also checked for site-packages (sys.base_prefix and sys.base_exec_prefix will always be the “real” prefixes of the Python installation). If “pyvenv.cfg” (a bootstrap configuration file) contains the key “include-system-site-packages” set to anything other than “true” (case-insensitive), the system-level prefixes will not be searched for site-packages; otherwise they will.

Copy link
Member

Choose a reason for hiding this comment

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

At least, it seems virtualenv does something with the pyvenv.cfg, if present (search in virtualenv github), but that needs more investigation to verify.

Copy link
Member

Choose a reason for hiding this comment

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

I mean, maybe there is a chance venv and virtualenv just doing the right thing if we put that pyvenv.cfg into the wrapped python folder. I don't know. What do you think? Is this something worth to investigate?

Copy link
Member Author

Choose a reason for hiding this comment

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

pyvenv.cfg is still Python 3-only, which is a downside. An alternative I've also considered is setting instead PYTHONUSERBASE. But that won't be sufficient for the non-withPackages case such as executables in applications.

Anyway, there is no clear solution to this problem because the problem we need to solve is not a typical problem for which the tooling has been designed. It's a matter of balancing things to come to a "sufficiently working" setup.

At least now by improving test coverage we can test whether any new method will really be better or not. And this change is an improvement.

Copy link
Member

Choose a reason for hiding this comment

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

At least now by improving test coverage we can test whether any new method will really be better or not. And this change is an improvement.

Agree to that.

Right now i started to get a clue on the python nixos tests.
I noticed we don't have integration tests for the cases we are in any virtual env, is this true or did i miss them?

I will try to extend that so that we can test 'import sqlite3' (for std lib) and 'import sitepkg' with any of the virtual env pythons.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, in pkgs/development/interpreters/python/tests/test_python.py. There it tests the prefixes for Nix envs, virtualenvs and venvs. That could be extended.

Note these are not NixOS tests, as in, they do not run in a VM.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, ok, then i will make a PR for that. Thanks, for the clarification also.

Copy link
Member

Choose a reason for hiding this comment

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

@FRidh i have done it. But am waiting right now until my local staging is build to check if everything applies. Btw. as the change somehow depends on the merged commits to staging should this PR be opened against staging or master?

Copy link
Member Author

Choose a reason for hiding this comment

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

against staging

Copy link
Member

Choose a reason for hiding this comment

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

Ay, ok. that i never did before and was scared because of so many changes when github compared against master by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants