これは、相互インポートの通常のケースです。次のレイアウトがあるとします
./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
そして、あなたは持っています
test.py
from one.two.three.four import e
1/2/3/4/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
paster を実行すると得られるものは次のとおりです。
$ 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)
最初のケースと 2 番目のケースの唯一の違いは、最初のケースが e のプログレッシブ インポートを示し、次に m のプログレッシブ インポートを示していることです。2 番目のケースでは、m を直接インポートします。
助けてくれてありがとう