Preferences

Nope. Relative imports work by relative package path, which is not at all the same. Often when you run Python you don't even have a package path.

Using `importlib` is a horrible hack that breaks basically all tooling. You very very obviously are not supposed to do that.


> Relative imports work by relative package path, which is not at all the same.

Exactly. This gives you the flexibility to distribute a complex package across multiple locations.

> Often when you run Python you don't even have a package path.

Any time you successfully import foo.bar, you necessarily have imported foo (because bar is an attribute of that object!), and therefore bar can `from . import` its siblings.

> Using `importlib` is a horrible hack that breaks basically all tooling. You very very obviously are not supposed to do that.

It is exactly as obvious (and true) that you are not "supposed to", in the exact same sense, directly specify where on disk the source code file you want to import is. After all, this constrains the import process to use a source code file. (Similarly if you specify a .pyc directly.) Your relative path doesn't necessarily make any sense after you have packaged and distributed your code and someone else has installed it. It definitely doesn't make any sense if you pack all your modules into a zipapp.

> Using `importlib` is a horrible hack that breaks basically all tooling. You very very obviously are not supposed to do that.

This is an assertion that has absolutely no reasoning behind it. I'm not saying I disagree; I'm just saying there is a time and a place for importlib.

Well not if you want high quality Python code. Pylint and Pyright won't understand it, and those are absolutely critical to writing Python code that works reliably.
> those are absolutely critical to writing Python code that works reliably.

Curious how much reliable Python code was written before those tools existed.

For that matter, curious how much was written before the `types` and `typing` standard library modules appeared.

This item has no comments currently.

Keyboard Shortcuts

Story Lists

j
Next story
k
Previous story
Shift+j
Last story
Shift+k
First story
o Enter
Go to story URL
c
Go to comments
u
Go to author

Navigation

Shift+t
Go to top stories
Shift+n
Go to new stories
Shift+b
Go to best stories
Shift+a
Go to Ask HN
Shift+s
Go to Show HN

Miscellaneous

?
Show this modal