The imp module and cyclic imports

M

Matthias Kramm

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
 
F

Fredrik Lundh

Matthias said:
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 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>
 
M

Matthias Kramm

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top