Importing modules from packages with relative imports

Discussion in 'Python' started by John Millikin, May 30, 2008.

  1. 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?
     
    John Millikin, May 30, 2008
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.