Enhance sitecustomize.py script installed by Python easyblock to fix support for virtualenv #2483
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(created using
eb --new-pr)virtualenvseems to bundle a customsite.pywhich in versions prior to 20.something does not havesite.getsitepackages(), see e.g. pypa/virtualenv#737The error in that case is
AttributeError: module 'site' has no attribute 'getsitepackages'The workaround is a bit hacky but seems to work across various versions of Python/virtualenv.
Some problems considered/solved:
base_prefixis supposed to contain the python prefix, but does not in early virtualenv versions, it instead contains the virtualenv prefixreal_prefixwas used instead but later removedgetsitepackagessite-packagesHence the very conservative approach of trying getting the first of all possible prefixes and moving all paths starting with that after the EBPYTHONPREFIXES
I also added a sanity check to check the base case, i.e. site-customize does work at all and adds the new site-packages path before the core site-packages path. (conservative check, we might want to limit to moving paths only ending with site-packages)
Testing that this works with virtualenv is hard, as we'd actually need to install a virtualenv. Not sure if we really want to do that.
The script I used to verify the new site-customize.py: checkPythonModule.sh