小编典典

是什么导致python模块两次导入?

python

据我了解,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()

此外,我还检查的ID
__builtin__.__import__。在主脚本的开头,我先导入__builtin__并打印,id(__builtin__.__import__)然后再进行其他任何导入。我还id(__builtin__.import__)从被多次导入的模块内部打印,并且id的值不变。

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


阅读 226

收藏
2020-12-20

共1个答案

小编典典

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

  • src /
    • 套餐1 /
    • 垃圾邮件
    • eggs.py

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

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

在这种情况下,您可以两次导入相同的模块,如下所示:

from package1 import spam
import spam

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

另外,根据下面的讨论(针对搜索此问题的用户),可以两次导入模块的另一种方法是在第一次导入过程中是否存在异常。例如,如果垃圾邮件导入了鸡蛋,但是导入鸡蛋导致
模块内部 出现异常,则可以再次将其导入。

2020-12-20