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 name form of import statement. these explicit relative imports use leading dots indicate current , parent packages involved in relative import. [...]


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -