这是相互导入的正常情况。假设您有以下布局
./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
你有
测试.py
from one.two.three.four import e
一/二/三/四/e.py
from one.two import m
一/二/m.py
print "m"
import u
一/二/u.py
print "u"
import m
当您运行 test.py 程序时,您当然期望:
python test.py
m
u
这是预期的行为。模块已经被导入,而且它们只有一次。在 Grok 中,这不会发生。假设有以下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
运行 paste 时获得的是:
$ 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
这是怎么回事?
从 pdb 堆栈跟踪中,这两种情况都是由 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)
第一种情况和第二种情况的唯一区别是第一种情况显示了 e 的渐进式导入,然后是 m 的渐进式导入。在第二种情况下,它直接导入 m。
谢谢您的帮助