The imp module and cyclic imports

Discussion in 'Python' started by Matthias Kramm, Nov 24, 2005.

  1. Hi All,

    I'm having a little bit of trouble using the "imp" module to
    dynamically import modules. It seems that somehow cyclic references of
    modules don't work.
    I'm unable to get the following to work:

    I've got the following files:

    web/__init__.py
    web/one.py
    web/two.py
    testimport.py

    >From which web/one.py contains the line:


    from web import two

    and web/two.py contains the line:

    from web import one

    ..

    Now I try to import one of the two modules in testimport.py:

    import imp
    (mfile,pathname,description) = imp.find_module("one", ["web"])
    m = imp.load_module("one",mfile,pathname,description)

    When I start the file testimport.py, I get the following exception:

    $ python testimport.py
    Traceback (most recent call last):
    File "testimport.py", line 3, in ?
    m = imp.load_module("one",mfile,pathname,description)
    File "web/one.py", line 1, in ?
    from web import two
    File "/scripts/python/importtest/web/two.py", line 2, in ?
    from web import one
    File "web/one.py", line 1, in ?
    from web import two
    ImportError: cannot import name two

    It seems that when two.py (referenced by one.py) references one.py
    again, something breaks.

    I'm lost. Am I doing something wrong, or is this a bug?

    Many thanks for any light anybody can shed on this.

    Greetings

    Matthias
     
    Matthias Kramm, Nov 24, 2005
    #1
    1. Advertising

  2. Matthias Kramm wrote:

    > I'm having a little bit of trouble using the "imp" module to
    > dynamically import modules. It seems that somehow cyclic
    > references of modules don't work.


    the problem you're seeing appears also if you use "import web.one"
    or "from web import one" or "__import__('web.one')".

    > I'm unable to get the following to work:
    >
    > I've got the following files:
    >
    > web/__init__.py
    > web/one.py
    > web/two.py
    > testimport.py
    >
    > >From which web/one.py contains the line:

    >
    > from web import two
    >
    > and web/two.py contains the line:
    >
    > from web import one


    I think, but I'm not 100% sure, that the problem you're seeing is
    that Python hasn't finished importing the "web.one" module when
    you're trying to import it again.

    - testimport wants to import web.one
    - python imports the web module
    - python finishes importing the web module
    - python imports the web.one module
    - web.one wants to import web.two
    - python imports the web.two module
    - web.two wants to import web.one
    - python notices that web.one is already being imported, and
    leaves it to the original import to finish the task

    if you replace the "from web import" statements with plain imports,
    everything will work as expected. just change

    from web import one

    to

    import one

    and do the same for the other module.

    hope this helps!

    </F>
     
    Fredrik Lundh, Nov 25, 2005
    #2
    1. Advertising

  3. > the problem you're seeing appears also if you use "import web.one"
    > or "from web import one" or "__import__('web.one')".


    Thanks for the hint. You're right. This isn't actually imp related. The
    standard import also fails.

    > if you replace the "from web import" statements with plain imports,
    > everything will work as expected. just change
    >
    > from web import one
    >
    > to
    >
    > import one


    Unfortunately, this fails if one.py and two.py are in different
    directories/packages.
    With a setup like
    web1/one.py
    web2/two.py
    there doesn't seem to be any way to make one.py and two.py reference
    each other via (non-delayed) imports.

    It's interesting, though, that cyclic imports work when using the plain
    "import foo" import, but not with the "from package import foo" style.
    Especially since the former also used to fail (google for "python
    cyclic imports" on groups.google.com). I wonder whether the "from"
    style imports were overlooked when the cyclic problem was fixed.

    Greetings

    Matthias
     
    Matthias Kramm, Nov 27, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James

    Cyclic imports

    James, Jun 27, 2008, in forum: Python
    Replies:
    4
    Views:
    391
    James
    Jun 27, 2008
  2. Stéphane Klein
    Replies:
    0
    Views:
    289
    Stéphane Klein
    Jul 5, 2011
  3. Replies:
    4
    Views:
    110
    Dave Angel
    Jul 4, 2013
  4. Cameron Simpson

    Re: Coping with cyclic imports

    Cameron Simpson, Jul 5, 2013, in forum: Python
    Replies:
    0
    Views:
    79
    Cameron Simpson
    Jul 5, 2013
  5. Oscar Benjamin

    Re: Coping with cyclic imports

    Oscar Benjamin, Jul 5, 2013, in forum: Python
    Replies:
    0
    Views:
    80
    Oscar Benjamin
    Jul 5, 2013
Loading...

Share This Page