The imp module and cyclic imports

2005-11-24 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The imp module and cyclic imports

2005-11-27 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list