Importing modules from packages with relative imports
With the old import system, and a package that looks like this:
foo/
__init__.py
main.py
bar/
__init__.py
baz.py
If I wanted to delay importing baz until it was actually used, I could
leave the __init__.py files empty and simply "import bar.baz".
However, with the new relative imports syntax, "from . import bar.baz"
causes a syntax error. I could use something like "from .bar import
baz as bar_baz", but that's long, annoying to write, and requires
changing all the uses of "bar.baz.spam" to "bar_baz.spam" through the
file. Is there any way to achieve the "bar.baz" name with relative
imports?
.
Relevant Pages
- Re: Creating classes and objects more than once?
... PEP 328 concerns itself with imports relative to the ... executing module in package space. ... which imports a Python module. ... (comp.lang.python) - Re: from __future__ import absolute_import issue
... > It appears that either absolute imports aren't working. ... > print string # Module imported is string.py in current directory, ... imports, relative to the package). ... (comp.lang.python) - Re: does the following code function as expected?
... List all imported packages and/or explicitilly named dotted classes ... If the first statement does not begin with the keyword `package', then there are no imports; otherwise, the first statement not beginning with `import' excluding the optional `package' statement is the end of imports. ... JIT compiler. ... (comp.lang.java.programmer) - Re: Module Structure/Import Design Problem
... layout, removing any structure, like it was before Python 1.5 added ... special syntax for accessing package-relative imports." ... only the module, or package top folder, needs to be in the pthon path ... for everything to work (which really means the parent folder is in the ... (comp.lang.python) - Re: Why we have to remove unused Import
... just for good code style? ... Using package imports can cause hidden problems. ... (comp.lang.java.programmer) |
|