-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Package imports don't really work #298
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
Comments
So, first thing is that classic Py packages require |
Agree that package import is not the cleanest thing. As you suggest, if we try to get standard packages working properly, and then just error out if we detect a namespace package, then I think that's a good way to have things initially. It's nice that namespace packages must not have |
Where do I start next? http://docs.python.org/3/library/functions.html#__import__ :
Note that sys.modules contains both package and package.module, so there's additional PITA how to do caching right, not just do loading right. |
…ported. See http://www.python.org/dev/peps/pep-0420/#specification for spec. See #298 for the discussion of the implemented behavior.
Ok, I pushed changes to get basic package imports work. Lot more to do. |
Thank you! |
Ok, going further. http://docs.python.org/3.3/library/functions.html#__import__ :
Implemented in fb7f943. Ok, now it's fair to say that basic package imports indeed work, only "advanced" left. |
Next one is: relative imports, http://legacy.python.org/dev/peps/pep-0328/ , http://docs.python.org/3/library/functions.html#__import__ level arg. So, it turns out these are not implemented on compiler level (I added at least a1aba36 to clarify that). So, leaving implementation on import() level to those who actually will need them, but added check to throw NotImplementedError. |
So, looking into CPython stdlib, relative imports are used widely - which is not surprising, because they resolve real import scoping problem. So, @dpgeorge , can you please schedule making them supported on compiler level. Not a big priority - I probably want be looking into further imports work near-term. |
oops, please ignore - was already implemented in 635543c ;-) |
Currently |
Latest case is partially resolved. Full resolution likely will require similar effort as implementing relative imports (there're issues how package paths resolved relative to different package modules). |
Thanks for your work on this. |
Actually - it was different issue, kind of recursive import. Should be fixed now, regardless of relative import stuff. |
Ok, basic relative imports are now implemented. There're likely various corner cases in it yet. This also should mark complete implementation of module-from-package import support. But not symbols-from-modules - likely, only top-level symbols can be imported now. And I wonder, how far Python goes with that? Can it import unbound method from a class? Bound method from an object? Overall, code is tangles and spread out between runtime.c and builtinimport.c, requires heavy refactoring (and that's not talking about verification for correctness). |
Ok, I finally came to real-world cases when namespace package are needed and thus learned details of them a bit ;-). The usecase actually pretty simple. Consider for example that Python3 has http.server and http.client modules, being under common "http" package. The unit of distribution and installation is a package, so we would need to force installation of both http.server and http.client at the same, and that's clear not acceptable exactly for us! So, idea of namespace packages is simple - just get rid of empty But it goes beyond that, allowing different package portions be installed at different paths as appearing in sys.path. This is not implemented. Per above, this requires recursive traversal of path with backtracking. CPython optimizes this by caching suitable subpaths at each namespace package's |
So, namespaces packages are cool things, using them a lot in micropython-lib. But uPy currently picks up first suitable dir as a namespace package, whereas CPy doesn't have conflicts when for example in current dir there's a subdir named as stdlib module - it will import stdlib package. But uPy will pick up this random subdir. Just wanted to note this as a known issue. |
Example: micropython/micropython-lib@6224013 |
What logic does CPy use to get around this? |
From memory of reading PEP, and based on behavior seen, it would be something like prioritizing normal modules/package over namespace packages (or perhaps over empty namespace packages). That would require building a candidate complete path list first, or scanning sys.path twice. We know where that (well, order of magnitude more such things) led CPython - to people complaining that CPython startup times are awful. So, not feeling like going there ;-) |
Relative imports currently currently appear to be broken on the board? I have created a test package as
importing pkg using the unix port works as expected, but on the board I get all lights on crash. If I adjust |
@cjbarnes18: The issue with relative imports now has its own issue at #872. |
As of now, there're no known issues with package imports remaining, closing. |
Even with all code added to builtinimport.c, package imports don't really work, which I kinda expected - package support is one of the messy parts of Python, few people know all details of how it works (me including).
I'm on it, and here's kinda extended commentary to changes I do.
The text was updated successfully, but these errors were encountered: