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
old-style (level=-1) importing broken after importlib changes #58797
Comments
Up to the early Py3.3 developer versions, calling __import__() with a level of -1 worked as in Python 2, i.e. it tried a relative import first and then a global import. This no longer seems to be available after the importlib rewrite (e.g. as of rev f341b99bb370). |
Relative imports are no longer supported in python 3, so this makes sense. |
By "relative" I meant "sibling". |
What Benjamin said. PEP-328 should have done away with relative imports, but somehow the __import__() function itself was not updated even though its docs were changed to say that index defaulted to 0. So this isn't actually a regressions because of importlib but actually just the implicit fixing of a bug. I was going to make sure to mention it in the "What's New" entry, but I will also add something to Misc/NEWS. |
I should also mention that the support for -1 indexing in Python 3 was weird because support was partially removed, but some 'if' checks only did |
It turns out that it wasn't all that hard to work around. Calling __import__ twice seems to do the trick. It's more overhead, but if people want speed, they can just be explicit about what kind of import they actually mean. So I wouldn't mind considering this an accidental left-over, and given that this behaviour has officially been removed for source code imports in 3.0 and taken out of documentation since then, it should be enough to consider its (partial) support in __import__ a bug and document it as being fixed in 3.3. |
Yeah, the fix is dead-simple, import with level=1 and if that fails import with level=0. I plan to cover this specific issue in the "What's New" for Python 3.3. |
With one caveat: relative imports don't work outside of packages, so the Interesting enough, I now get this when trying it at the prompt: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: error return without exception set |
Hmm, interesting - it stripped the command from my e-mail. I was doing this: >>> __import__("sys", level=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: error return without exception set |
Detecting if you are in a package is as simple as As for the failure, I will have a look. |
New changeset 68f9ad6a3b13 by Brett Cannon in branch 'default': |
SystemError fixed (stemming from a misunderstanding of what PyDict_GetItemWithError() did). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: