Ian*_*low 14 python import module
据我所知,python模块永远不会导入两次,即模块中的代码仅在第一次导入时执行.后续导入语句只是将模块添加到导入范围.
我有一个名为"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()
Run Code Online (Sandbox Code Playgroud)
它继续执行几次.但是,第二次调用它的完整堆栈跟踪没有显示任何调用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()
Run Code Online (Sandbox Code Playgroud)
而且,我也检查了身份证__builtin__.__import__.在我的主要脚本的最开始,我在进行任何其他导入之前导入__builtin__并打印id(__builtin__.__import__).我还id(__builtin__.import__)从我的模块中打印多次导入,并且id的值不会改变.
除了调用重载和覆盖之外还有其他机制__builtin__.__import__可以解释我的模块多次加载吗?
Ken*_*der 27
如果在路径中找到模块两次,则可以导入两次Python模块.例如,假设您的项目布局如下:
假设您的PYTHONPATH(sys.path)包含src和src/package1:
PYTHONPATH=/path/to/src:/path/to/src/package1
Run Code Online (Sandbox Code Playgroud)
如果是这种情况,您可以像这样导入相同的模块两次:
from package1 import spam
import spam
Run Code Online (Sandbox Code Playgroud)
Python会认为它们是不同的模块.那是怎么回事?
此外,根据下面的讨论(对于搜索此问题的用户),模块可以导入两次的另一种方式是在第一次导入的中途有异常.例如,如果垃圾邮件导入鸡蛋,但导入鸡蛋导致模块内部出现异常,则可以再次导入.
| 归档时间: |
|
| 查看次数: |
6859 次 |
| 最近记录: |