-
Notifications
You must be signed in to change notification settings - Fork 81
Description
Background
In pandas, we have a source directory structure that looks like this.
pandas
pandas
__init__.py
...
_libs
Extension modules
...
Usually, with setuptools, we build the extension modules inplace, and importing pandas from the source directory works fine because of this.
However, with meson-python, we build/install pandas to the site-packages directory(with pip install . -v --no-build-isolation --config-settings="builddir=builddir" ). Since the build is now out-of-tree, import pandas within the source directory, fails (expectedly), since we can't find the extension modules in the source directory anymore.
This is a massive PITA, since now you can't work/debug on pandas using the Python interpreter in the base of the source directory anymore.
Solution
(Disclaimer: I'm still very confused by how the Python import system works internally, so I'm probably wrong/missing something somewhere).
The problem is mainly caused by python pre-pending the current working dir to the front of sys.path. To fix this, we'd need to not load our pandas from there, and instead from the one installed in site-packages.
In order to execute code/modify sys.path before the interpreter initializes the module search path, we have two options which are the .pth files and sitecustomize.py.
Unfortunately, the .pth files and sitecustomize.py seem to be run before Python prepends the current working directory to the front of sys.path, making it impossible to delete.
A workaround to this would be to inject a new MetaPathFinder(maybe can inherit from PathFinder?) into sys.meta_path, to detect when e.g. pandas is being imported from its source directory. Since .pth files don't support executing arbritrary code well(I think it's going to be deprecated sometime), sitecustomize.py is probably the place to put this code.
This would require meson-python to be responsible for writing/appending to this sitecustomize.py file, which is why I'm asking for feedback.
cc @mesonbuild/meson-python-devs and @stefanv (this might be related to the devpy python shell not working on < 3.11)