-
Notifications
You must be signed in to change notification settings - Fork 11
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
Advice about import hook limitation #21
Comments
Hi @abravalheri I actually think this is possibly simpler than you suggest - it looks basically like a simple case of having two different I'd be tempted to say the simple solution here is "don't name your project directory the same as the package name in a monorepo". Does that fix the issue? If it doesn't, I'd be very curious as to why. As regards this being a counter-argument for import hooks, I think it is (in the sense that it's a case where import hooks appear to have unexpected edge cases). I don't personally think that's a disaster, as I consider the I'd be happy to improve the hook-based approach, but I expect it will be a long time before I'd consider making it the recommended approach. I know I'm holding it to a higher standard than |
Thank you very much @pfmoore for the insights. At some point I want to experiment a bit more with |
Reopening as a reminder for myself. I do want to investigate this scenario more closely, and I think it's useful to have a discussion about import hooks and their limitations. I think the key points for me at the moment are:
To be perfectly honest, I think that some of these issues are inherent in the design of the existing path hook mechanism1, and we're only hitting them now because outside of zipimport (which uses a path hook, not a meta hook) there's very little use of import hooks, so the edge cases haven't got exposed until now. I wonder whether there is merit in looking at a mechanism similar to the existing Footnotes |
The investigation that I had in mind was in this direction. I was thinking about implementing a path hook that mimics the zipimport mechanism. I can imagine a My hopes are that by using a |
You may still have issues regarding (3), because the But yes, I quite like this idea. And the fact that both you and I came up with something similar suggests that it has potential 🙂 I don't have much time at the moment to work on this, but yes, please, let me know how you get on (and if you need help, feel free to ask). |
I've been looking at this issue again. I'm trying to understand what the actual problem is with namespace packages. The root of my confusion is that basically, a top level non-namespace package is (in effect) "owned" by a single project. So you can't have two projects "foo" and "bar" that both own the top-level name "foobar". Or rather, you can, but the packaging machinery doesn't protect you from any mess that you get in (you can install them both, but what you get is a mess with the two projects overwriting each other). With that in mind, I'm embarrassed to admit that it's possible the "namespace issue" simply isn't an issue in practice, and I've been spreading FUD 🙁 I'm pretty sure it's possible to create a top-level namespace package using a path hook (I have a proof of concept, but it is very manual, so I won't post it here). That "solves" the problem of how to install, in editable mode, a namespace package. But that's not the way I see people typically discussing namespace packages - they seem to be looking more at something along the lines of installing a project So in reality, I don't understand the actual use case for namespace packages in wheels. @abravalheri I assume you have more experience with people actually wanting to use namespace packages in "real life". Can you explain to me what I've missed here? |
Hi Paul, thank you very much for the project.
It was really important for me as a reference when implementing PEP 660 support in setuptools.
I still plan to contribute to editables in terms of namespace handling once the details are flashed out (this may depend on the instructions we get in
https://github.com/python/cpython/issues/92054
).I am opening this issue to seek for advice/have a general discussion about using import hooks in editable installs.
Recently in the
setuptools
repository we got an issue reporting that the editable installation of a regular package (ie. non-namespace) don't behave in the same way as a normal installation.I understand that
editables
would also have the same problem. So I wanted to share with you this particular use case and check what are your thoughts.Problem description
Let's imagine a monorepo that contains multiple directories, each one of them corresponding to a Python project, like the following:
When the users perform a regular installation of one sub-directory, they are able to run scripts importing the installed package; even when the "current working directory (CWD)" is the root of the monorepo.
This is demonstrated in the snippet bellow:
In normal conditions, "stuff" in the CWD should take precedence over "stuff" in site-packages.
However, this rule does not seem to apply for namespace packages in CWD.
If I had to make a wild guess, I would say that the following is happening:
ModuleSpec
for the namespace in CWDModuleSpec
with a new (regular) one pointing to the directory with__init__.py
.When using a import hook/meta path finder, the behaviour differs, and the users are surprised with an error:
In this scenario the "accidental" namespace package created by having
""
onsys.path
takes precedence over the import hook.Since the objective of an editable install is to operate as close as possible to a regular install, this use case could be used as a counter argument for import hooks.
The text was updated successfully, but these errors were encountered: