Relative/Absolute Wildcard Imports #2957
Replies: 1 comment 3 replies
-
As you point out, you can disable the Out of curiosity, why would you use absolute imports within a library to refer to its own submodules? This approach depends on proper installation of the library, which means these imports won't work development or in other situations where the library hasn't yet been installed in the active Python environment. It also makes your library vulnerable to unexpected module imports based on dynamic search paths in the user's environment. I've seen a few libraries use this approach, but I wouldn't recommend it. Relative imports are much better here, IMO. |
Beta Was this translation helpful? Give feedback.
-
Why are relative and absolute imports treated differently for wildcard library error rule? I have a library I work on,
and if I write in
__init__.py
from .a import *
from .b import *
I get no errors. But if I write,
from foo.a import *
from foo.b import *
I get wildcard import errors. Style wise my codebase chose to always require absolute imports. I can ignore that rule, but don't really understand why relative imports should be allowed for defining public api of a library but not absolute imports.
I could also disable wildcard rule although I feel a bit meh there as I agree with the rule on don't import * other libraries (although file level disable helps here). But importing * for your own library for defining api feels safe to me and a lot less duplication then importing several dozen names.
This feels too minor for a bug report given the fallback option of just disable rule in few files that handle exporting public api. Also partly because I'm unsure if this relative vs absolute difference is intentional or a bug.
Beta Was this translation helpful? Give feedback.
All reactions