什么可能导致python模块导入两次?

时间:2011-01-25 20:42:12

标签: python module import

据我所知,python模块永远不会导入两次,即模块中的代码仅在第一次导入时执行。后续的import语句只是将模块添加到导入范围。

我有一个名为“TiledConvC3D.py”的模块,但似乎已多次导入。我使用pdb打印此模块代码顶部的堆栈。

这是从第一次执行模块开始的堆栈跟踪结束:

File "<anonymized>/python_modules/Theano/theano/gof/cmodule.py", line 328, in refresh
  key = cPickle.load(open(key_pkl, 'rb'))
File "<anonymized>/ops/TiledConvG3D.py", line 565, in <module>
  import TiledConvC3D
File "<anonymized>/ops/TiledConvC3D.py", line 18, in <module>
  pdb.traceback.print_stack()

它继续执行几次。但是,第二次调用它的完整堆栈跟踪不会显示对reload的任何调用,因此不应执行这些执行:

File "sup_train_conj_grad.py", line 103, in <module>
  dataset = Config.get_dataset(dataset_node)
File "<anonymized>/Config.py", line 279, in get_dataset
  from datasets import NewWiskott
File "<anonymized>/datasets/NewWiskott.py", line 16, in <module>
  normalizer_train = video.ContrastNormalizer3D(sigma, global_per_frame = False, input_is_5d = True)
File "<anonymized>/util/video.py", line 204, in __init__
  self.f = theano.function([input],output)
File "<anonymized>/python_modules/Theano/theano/compile/function.py", line 105, in function
  allow_input_downcast=allow_input_downcast)
File "<anonymized>/python_modules/Theano/theano/compile/pfunc.py", line 270, in pfunc
  accept_inplace=accept_inplace, name=name)
File "<anonymized>/python_modules/Theano/theano/compile/function_module.py", line 1105, in orig_function
  fn = Maker(inputs, outputs, mode, accept_inplace = accept_inplace).create(defaults)
File "/u/goodfeli/python_modules/Theano/theano/compile/function_module.py", line 982, in create
  _fn, _i, _o = self.linker.make_thunk(input_storage = input_storage_lists)
File "<anonymized>/python_modules/Theano/theano/gof/link.py", line 321, in make_thunk
  output_storage = output_storage)[:3]
File "<anonymized>/python_modules/Theano/theano/gof/cc.py", line 1178, in make_all
  output_storage = node_output_storage)
File "<anonymized>/python_modules/Theano/theano/gof/cc.py", line 774, in make_thunk
  cthunk, in_storage, out_storage, error_storage = self.__compile__(input_storage, output_storage)
File "<anonymized>/python_modules/Theano/theano/gof/cc.py", line 723, in __compile__
  output_storage)
File "<anonymized>/python_modules/Theano/theano/gof/cc.py", line 1037, in cthunk_factory
  module = get_module_cache().module_from_key(key=key, fn=self.compile_cmodule)
File "<anonymized>/python_modules/Theano/theano/gof/cc.py", line 59, in get_module_cache
  return cmodule.get_module_cache(config.compiledir)
File "<anonymized>/python_modules/Theano/theano/gof/cmodule.py", line 576, in get_module_cache
  _module_cache = ModuleCache(dirname, force_fresh=force_fresh)
File "<anonymized>/python_modules/Theano/theano/gof/cmodule.py", line 268, in __init__
  self.refresh()
File "<anonymized>/python_modules/Theano/theano/gof/cmodule.py", line 326, in refresh
  key = cPickle.load(open(key_pkl, 'rb'))
File "<anonymized>/ops/TiledConvV3D.py", line 504, in <module>
  import TiledConvG3D
File "<anonymized>/ops/TiledConvG3D.py", line 565, in <module>
  import TiledConvC3D
File "<anonymized>/ops/TiledConvC3D.py", line 22, in <module>
  pdb.traceback.print_stack()

此外,我还会检查__builtin__.__import__的ID。在我的主脚本开始时,我导入__builtin__并在执行任何其他导入之前打印id(__builtin__.__import__)。我还从我的模块中打印多次id(__builtin__.import__),并且id的值不会改变。

除了调用重新加载和覆盖__builtin__.__import__之外还有其他机制可以解释我的模块多次加载吗?

1 个答案:

答案 0 :(得分:23)

如果在路径中找到模块两次,则可以导入两次Python模块。例如,假设您的项目布局如下:

  • 的src /
    • 包1 /
      • spam.py
      • eggs.py

假设您的PYTHONPATH(sys.path)包含src和src / package1:

PYTHONPATH=/path/to/src:/path/to/src/package1

如果是这种情况,您可以像这样导入相同的模块两次:

from package1 import spam
import spam

Python会认为它们是不同的模块。那是怎么回事?

此外,根据下面的讨论(对于搜索此问题的用户),模块可以导入两次的另一种方式是在第一次导入的中途发生异常。例如,如果垃圾邮件导入鸡蛋,但导入鸡蛋导致模块内的异常,则可以再次导入。