双重进口

2024-05-19 08:10:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这是相互影响的正常情况。假设您有以下布局

./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在

^{pr2}$

一个/两个/三个/四个/e.py

from one.two import m

一次/两次/次

print "m"
import u

一个/两个/u.py

print "u"
import m

当你运行测试.py程序,你当然希望:

python test.py
m
u

这是预期的行为。模块已经导入,并且只导入一次。在摸索中,这不会发生。假设有以下内容应用程序副本在

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

运行“粘贴器”时获得的结果是:

$ 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堆栈跟踪中,两个案例都是由火星人导入的:

    /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

谢谢你的帮助


Tags: pyimportgrokinitbuildout情况oneusers

热门问题