Python Relative Imports and Packages -
i'm trying create package , have tree structure looks this:
dionesus/ setup.py dionesus/ __init__.py dionesus.py
dionesus.py has class called dionesus. init.py empty.
how import class dionesus without having specify top level folder?
i have do:
import dionesus d = dionesus.dionesus.dionesus()
i import statements like:
import dionesus d = dionesus.dionesus()
first, can still use absolute import, using from … import
form:
from dionesus import dionesus d = dionesus.dionesus()
this problematic if ever need import both dionesus , dionesus.dionesus in same module, that's pretty implicit in desire give them both same non-disambiguated name…
alternatively, if you're in parent or sibling or other relative of dionesus.dionesus, can use relative import. depending on are, it'll different (that's relative means, after all); may importing .
, .dionesus
, ..
, etc. wherever is, it's same from … import
form above, relative name instead of absolute one. (in fact, relative imports always use from
form.)
from . import dionesus d = dionesus.dionesus()
Comments
Post a Comment