By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

The imp module and cyclic imports

P: n/a
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

Nov 24 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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>

Nov 25 '05 #2

P: n/a
> 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

Nov 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.