tags:

views:

68

answers:

1

This is a normal case of mutual import. Suppose you have the following layout

./test.py
./one
./one/__init__.py
./one/two
./one/two/__init__.py
./one/two/m.py
./one/two/three
./one/two/three/__init__.py
./one/two/three/four
./one/two/three/four/__init__.py
./one/two/three/four/e.py
./one/two/u.py

And you have

test.py

 from one.two.three.four import e

one/two/three/four/e.py

from one.two import m

one/two/m.py

print "m"
import u

one/two/u.py

print "u"
import m

When you run the test.py program, you expect, of course:

python test.py
m
u

Which is the expected behavior. Modules have already been imported, and they are only once. In Grok, this does not happen. Suppose to have the following app.py

import os; import sys; sys.path.insert(1,os.path.dirname( os.path.realpath( __file__ ) ))
import grok
from one.two.three.four import e

class Sample(grok.Application, grok.Container):
    pass

what you obtain when you run paster is:

$ bin/paster serve parts/etc/deploy.ini
2009-10-07 15:26:57,154 WARNING [root] Developer mode is enabled: this is a security risk and should NOT be enabled on production servers. Developer mode can be turned off in etc/zope.conf
m
u
m
u

What's going on in here ?

from a pdb stack trace, both cases are imported by martian:

    /Users/sbo/.buildout/eggs/martian-0.11-py2.4.egg/martian/core.py(204)grok_package()  
  -> grok_module(module_info, grokker, **kw)                                             
    /Users/sbo/.buildout/eggs/martian-0.11-py2.4.egg/martian/core.py(209)grok_module()   
  -> grokker.grok(module_info.dotted_name, module_info.getModule(),                      
    /Users/sbo/.buildout/eggs/martian-0.11-py2.4.egg/martian/scan.py(118)getModule()     
  -> self._module = resolve(self.dotted_name)                                            
    /Users/sbo/.buildout/eggs/martian-0.11-py2.4.egg/martian/scan.py(191)resolve()       
  -> __import__(used)

The only difference between the first case and the second one is that the first shows the progressive import of e and then of m. In the second case it directly imports m.

Thanks for the help

A: 

This could possibly be a side-effect of the introspection Grok does, I'm not sure.

Try to put a pdb.set_trace() in m, and check at the stack trace to see what is importing the modules.

Lennart Regebro
It appears that it's due to my strategy to import the package. I'm working around it, but still it's a rather anomalous behavior. I found it out because one of the modules pushes an entry in a dict that raises if a duplicate is introduced, and when the second import happens, I get an exception.
Stefano Borini