What could cause a python module to be imported twice?
据我所知,python模块从不导入两次,即模块中的代码只在第一次导入时执行。随后的import语句只是将模块添加到导入的范围中。
我有一个叫做"tiledconvc3d.py"的模块,它似乎被多次导入。我使用pdb打印这个模块代码顶部的堆栈。
以下是第一次执行模块时的堆栈跟踪结束:
1 2 3 4 5 6 | File"/python_modules/Theano/theano/gof/cmodule.py", line 328, in refresh key = cPickle.load(open(key_pkl, 'rb')) File"/ops/TiledConvG3D.py", line 565, in <module> import TiledConvC3D File"/ops/TiledConvC3D.py", line 18, in <module> pdb.traceback.print_stack() |
它还会被执行几次。但是,第二次调用的完整堆栈跟踪没有显示对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | File"sup_train_conj_grad.py", line 103, in <module> dataset = Config.get_dataset(dataset_node) File"/Config.py", line 279, in get_dataset from datasets import NewWiskott File"/datasets/NewWiskott.py", line 16, in <module> normalizer_train = video.ContrastNormalizer3D(sigma, global_per_frame = False, input_is_5d = True) File"/util/video.py", line 204, in __init__ self.f = theano.function([input],output) File"/python_modules/Theano/theano/compile/function.py", line 105, in function allow_input_downcast=allow_input_downcast) File"/python_modules/Theano/theano/compile/pfunc.py", line 270, in pfunc accept_inplace=accept_inplace, name=name) File"/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"/python_modules/Theano/theano/gof/link.py", line 321, in make_thunk output_storage = output_storage)[:3] File"/python_modules/Theano/theano/gof/cc.py", line 1178, in make_all output_storage = node_output_storage) File"/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"/python_modules/Theano/theano/gof/cc.py", line 723, in __compile__ output_storage) File"/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"/python_modules/Theano/theano/gof/cc.py", line 59, in get_module_cache return cmodule.get_module_cache(config.compiledir) File"/python_modules/Theano/theano/gof/cmodule.py", line 576, in get_module_cache _module_cache = ModuleCache(dirname, force_fresh=force_fresh) File"/python_modules/Theano/theano/gof/cmodule.py", line 268, in __init__ self.refresh() File"/python_modules/Theano/theano/gof/cmodule.py", line 326, in refresh key = cPickle.load(open(key_pkl, 'rb')) File"/ops/TiledConvV3D.py", line 504, in <module> import TiledConvG3D File"/ops/TiledConvG3D.py", line 565, in <module> import TiledConvC3D File"/ops/TiledConvC3D.py", line 22, in <module> pdb.traceback.print_stack() |
此外,我还检查了
除了调用reload和重写
如果路径中有两个python模块,则可以导入两次。例如,假设项目的布局如下:
- SRC/
- 包装1
- P.P.Py
- 蛋黄酱
- 包装1
假设您的pythonpath(sys.path)包含src和src/package1:
1 | PYTHONPATH=/path/to/src:/path/to/src/package1 |
如果是这样,您可以像这样导入同一个模块两次:
1 2 | from package1 import spam import spam |
而python会认为它们是不同的模块。这是怎么回事?
此外,根据下面的讨论(对于搜索此问题的用户),模块可以两次导入的另一种方法是,如果在第一次导入的中途出现异常。例如,如果垃圾邮件导入鸡蛋,但导入鸡蛋会导致模块内部出现异常,则可以再次导入。