Skip to content

Python: query distutils to find site-packages directory#24095

Merged
tgamblin merged 6 commits intospack:developfrom
adamjstewart:packages/python
Jul 16, 2021
Merged

Python: query distutils to find site-packages directory#24095
tgamblin merged 6 commits intospack:developfrom
adamjstewart:packages/python

Conversation

@adamjstewart
Copy link
Copy Markdown
Member

@adamjstewart adamjstewart commented Jun 3, 2021

Third-party Python libraries may be installed in one of several directories:

  1. lib/pythonX.Y/site-packages for Spack-installed Python
  2. lib64/pythonX.Y/site-packages for system Python on RHEL/CentOS/Fedora
  3. lib/pythonX/dist-packages for system Python on Debian/Ubuntu

Previously, Spack packages were hard-coded to use the (1). Now, we query the Python installation itself and ask it which to use. Ever since #21446 this is how we've been determining where to install Python libraries anyway.

Note: there are still many packages that are hard-coded to use (1). I can change them in this PR, but I don't have the bandwidth to test all of them.

Fixes #24076
Fixes #24526

@skosukhin @permeakra @wspear can you test this?

Copy link
Copy Markdown
Contributor

@permeakra permeakra left a comment

Choose a reason for hiding this comment

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

still doesn't work. I think, tuning the glob rule is required. After switching to this pr branch, I got cython libs installed into

/home/permeakra/spack/opt/spack/linux-ubuntu20.04-haswell/gcc-9.3.0/py-cython-0.29.22-iyt6swlafaunff4uawkj7w6adtq2wusm/lib/python3/dist-packages/

but a directory

/home/permeakra/spack/opt/spack/linux-ubuntu20.04-haswell/gcc-9.3.0/py-cython-0.29.22-iyt6swlafaunff4uawkj7w6adtq2wusm/lib/python3.8/site-packages/

still existed and was pulled into PYTHONPATH instead.

@adamjstewart

This comment has been minimized.

@wspear

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@wspear

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@wspear

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@wspear

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@wspear

This comment has been minimized.

@adamjstewart

This comment has been minimized.

@permeakra

This comment has been minimized.

@adamjstewart adamjstewart changed the title Python: handle dist-packages and site-packages Python: query distutils to find site-packages directory Jun 10, 2021
@adamjstewart adamjstewart requested a review from alalazo June 10, 2021 03:33
@adamjstewart
Copy link
Copy Markdown
Member Author

Can everyone test this latest version? You don't need to rebuild anything, just try to use an external Python with this latest commit. I was able to successfully install py-numpy and the PYTHONPATH was set up correctly.

It looks like some of the unit tests are failing because we install a fake python binary that can't actually be run. Still need to fix this.

@wspear
Copy link
Copy Markdown
Contributor

wspear commented Jun 10, 2021

After double checking that the 'before' state failed, applying this this change made my py-numpy install work. Looks good to me.

@adamjstewart
Copy link
Copy Markdown
Member Author

@alalazo to get the test to pass, I think there's two approaches we could take:

  1. Monkeypatch the test so we don't try to run python to query where site_packages_dir is during activation
  2. Use a try-except in site_packages_dir and fallback to lib/pythonX.Y/site-packages if python crashes

(2) might be helpful in the case where someone is using an external Python and distutils is not installed (I think I remember someone reporting a case like this, was it @michaelkuhn?)

@adamjstewart
Copy link
Copy Markdown
Member Author

@alalazo can you explain the clingo test failure?

@alalazo
Copy link
Copy Markdown
Member

alalazo commented Jun 17, 2021

can you explain the clingo test failure?

For clingo unit tests we currently bootstrap clingo from sources (with cmake and bison detected as externals to cut on the wall-clock time). Apparently this PR breaks Spack's ability to build an importable clingo module. By just looking at logs it seems here Spack wrongly detects the current Python interpreter to be Python 3.8 instead of Python 2.7 in the failing job.

@adamjstewart
Copy link
Copy Markdown
Member Author

Any idea how the changes in this PR could have broken that? I didn't change any of the detection logic.

@alalazo
Copy link
Copy Markdown
Member

alalazo commented Jun 17, 2021

Any idea how the changes in this PR could have broken that? I didn't change any of the detection logic.

No, I need to try reproduce the issue.

@adamjstewart
Copy link
Copy Markdown
Member Author

@spackbot run pipeline

@spackbot-app
Copy link
Copy Markdown

spackbot-app bot commented Jul 16, 2021

I've started that pipeline for you!

@adamjstewart
Copy link
Copy Markdown
Member Author

Style tests are now fixed. Failing clingo builds are also now magically fixed? I'm suspicious about that, but won't question it if it helps this PR to get finally merged.

@adamjstewart
Copy link
Copy Markdown
Member Author

@spackbot run pipeline

@spackbot-app
Copy link
Copy Markdown

spackbot-app bot commented Jul 16, 2021

I've started that pipeline for you!

@tgamblin tgamblin enabled auto-merge (squash) July 16, 2021 13:19
@adamjstewart
Copy link
Copy Markdown
Member Author

@tgamblin can you re-review?

@tgamblin tgamblin merged commit c37df94 into spack:develop Jul 16, 2021
@adamjstewart adamjstewart deleted the packages/python branch July 16, 2021 15:28
alalazo added a commit to alalazo/spack that referenced this pull request Jul 21, 2021
spack#24095 introduced a couple of bugs, which are fixed here:

1. The module path is computed incorrectly for bootstrapped clingo
2. We remove too many paths for `sys.path` in case of failures
tgamblin pushed a commit that referenced this pull request Jul 21, 2021
#24095 introduced a couple of bugs, which are fixed here:

1. The module path is computed incorrectly for bootstrapped clingo
2. We remove too many paths for `sys.path` in case of failures
ajaust pushed a commit to ajaust/spack that referenced this pull request Jul 22, 2021
spack#24095 introduced a couple of bugs, which are fixed here:

1. The module path is computed incorrectly for bootstrapped clingo
2. We remove too many paths for `sys.path` in case of failures
alalazo added a commit that referenced this pull request Aug 3, 2021
Add a workflow to test bootstrapping clingo on 
different platforms so that we can detect changes 
that break it.

Compute `site_packages_dir` in `bootstrap.py` as it was
before #24095, until we figure a better way to override
that attribute.
@adamjstewart
Copy link
Copy Markdown
Member Author

Unfortunately, it looks like we're going to have to find a different way to do this. Python 3.10 is deprecating distutils and it will be removed completely in 3.12: https://docs.python.org/3.10/whatsnew/3.10.html#distutils

@alalazo
Copy link
Copy Markdown
Member

alalazo commented Aug 16, 2021

I'll try to think of a solution, let me know if you have ideas. We need something like this with a package override to finish #25371 (the only thing missing there would be bootstrapping pytest for developers).

tgamblin pushed a commit that referenced this pull request Aug 26, 2021
This is a direct followup to #13557 which caches additional attributes that were added in #24095 that are expensive to compute. I had to reopen #25556 in another PR to invalidate the GitLab CI cache, but see #25556 for prior discussion.

### Before

```console
$ time spack env activate .

real	2m13.037s
user	1m25.584s
sys	0m43.654s
$ time spack env view regenerate
==> Updating view at /Users/Adam/.spack/.spack-env/view

real	16m3.541s
user	10m28.892s
sys	4m57.816s
$ time spack env deactivate

real	2m30.974s
user	1m38.090s
sys	0m49.781s
```

### After
```console
$ time spack env activate .

real	0m8.937s
user	0m7.323s
sys	0m1.074s
$ time spack env view regenerate
==> Updating view at /Users/Adam/.spack/.spack-env/view

real	2m22.024s
user	1m44.739s
sys	0m30.717s
$ time spack env deactivate

real	0m10.398s
user	0m8.414s
sys	0m1.630s
```

Fixes #25555
Fixes #25541 

* Speedup environment activation, part 2
* Only query distutils a single time
* Fix KeyError bug
* Make vermin happy
* Manual memoize
* Add comment on cross-compiling
* Use platform-specific include directory
* Fix multiple bugs
* Fix python_inc discrepancy
* Fix import tests
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.

Installation issue: py-cython "error: option --single-version-externally-managed not recognized" Installation issue: py-numpy

6 participants