Importing modules from packages with relative imports

J

John Millikin

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top