gemäß PEP8 sind relative Imports ja zu vermeiden. Allerdings habe ich heute hier folgendes gelesen:
Nach meinem Verständnis heißt das, dass einThe only acceptable syntax for relative imports is from .[module] import name. All import forms not starting with . are interpreted as absolute imports. (PEP 0328)
Code: Alles auswählen
from package import module
Code: Alles auswählen
from .package import module
So wie es aussieht, ist genau das der Fall, wie folgender Test zeigt:
Verzeichnisstruktur:
Code: Alles auswählen
/tmp/pytest/
|- __init__.py
|- xml.py
|- loremipsum.py
Code: Alles auswählen
__all__ = ['tkinter']
import tkinter
print(tkinter)
import loremipsum
print(loremipsum)
Code: Alles auswählen
print("I'm imported!\n")
Code: Alles auswählen
>>> import pytest
<module 'tkinter' from '/usr/lib/python3.2/tkinter/__init__.py'>
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "pytest/__init__.py", line 6, in <module>
import loremipsum
ImportError: No module named loremipsum
Hat jemand einfach nur vergessen, PEP8 zu aktualisieren oder gibt es noch einen anderen Weg, eigene Module zu importieren (außer mit importlib.import_module())?Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that PEP 328 is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.