python: alias for relative import -
is possible import module of same package alias using relative import?
say have following package structure:
lib/ foobar/ __init__.py foo.py bar.py and in foo.py, i'd use bar.py, i'd use "bar.my_function", instead of from .bar import my_function, i've tried import .bar bar , import .bar, both of don't work (invalid syntax exception). i've tried both pythhon2.7 , python3.4 (the latter being target version).
however, work, , i'm using now, import foobar.bar bar, i.e. absolute import instead of relative. it's ok solution, given don't expect package name change (and if does, there's not change in code), nice if accomplish using relative import!
summary:
#import .bar bar # why not?!? #import .bar # shot in dark import foobar.bar bar # current solution
you need use
from . import bar the documentation states concerning this
[...] can write explicit relative imports
from module import nameform of import statement. these explicit relative imports use leading dots indicate current , parent packages involved in relative import. [...]
Comments
Post a Comment