title: What could cause a python module to be imported twice?
body: <p>As far as I understand, a python module is never imported twice, i.e. the code in the module only gets executed the first time it is imported. Subsequent import statements just add the module to the scope of the import.</p>
  
  <p>I have a module called "TiledConvC3D.py" that seems to be imported multiple times though. I use pdb to print the stack at the top of the code for this module.</p>
  
  <p>Here is the end of the stack trace from the first time the module is executed:</p>
  
  <pre><code>File "&lt;anonymized&gt;/python_modules/Theano/theano/gof/cmodule.py", line 328, in refresh
    key = cPickle.load(open(key_pkl, 'rb'))
  File "&lt;anonymized&gt;/ops/TiledConvG3D.py", line 565, in &lt;module&gt;
    import TiledConvC3D
  File "&lt;anonymized&gt;/ops/TiledConvC3D.py", line 18, in &lt;module&gt;
    pdb.traceback.print_stack()
  </code></pre>
  
  <p>It goes on to be executed several more times. However, the complete stack trace for the second time it is called does not show any calls to <code>reload</code>, so these executions should not be occurring:</p>
  
  <pre><code>File "sup_train_conj_grad.py", line 103, in &lt;module&gt;
    dataset = Config.get_dataset(dataset_node)
  File "&lt;anonymized&gt;/Config.py", line 279, in get_dataset
    from datasets import NewWiskott
  File "&lt;anonymized&gt;/datasets/NewWiskott.py", line 16, in &lt;module&gt;
    normalizer_train = video.ContrastNormalizer3D(sigma, global_per_frame = False, input_is_5d = True)
  File "&lt;anonymized&gt;/util/video.py", line 204, in __init__
    self.f = theano.function([input],output)
  File "&lt;anonymized&gt;/python_modules/Theano/theano/compile/function.py", line 105, in function
    allow_input_downcast=allow_input_downcast)
  File "&lt;anonymized&gt;/python_modules/Theano/theano/compile/pfunc.py", line 270, in pfunc
    accept_inplace=accept_inplace, name=name)
  File "&lt;anonymized&gt;/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 "&lt;anonymized&gt;/python_modules/Theano/theano/gof/link.py", line 321, in make_thunk
    output_storage = output_storage)[:3]
  File "&lt;anonymized&gt;/python_modules/Theano/theano/gof/cc.py", line 1178, in make_all
    output_storage = node_output_storage)
  File "&lt;anonymized&gt;/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 "&lt;anonymized&gt;/python_modules/Theano/theano/gof/cc.py", line 723, in __compile__
    output_storage)
  File "&lt;anonymized&gt;/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 "&lt;anonymized&gt;/python_modules/Theano/theano/gof/cc.py", line 59, in get_module_cache
    return cmodule.get_module_cache(config.compiledir)
  File "&lt;anonymized&gt;/python_modules/Theano/theano/gof/cmodule.py", line 576, in get_module_cache
    _module_cache = ModuleCache(dirname, force_fresh=force_fresh)
  File "&lt;anonymized&gt;/python_modules/Theano/theano/gof/cmodule.py", line 268, in __init__
    self.refresh()
  File "&lt;anonymized&gt;/python_modules/Theano/theano/gof/cmodule.py", line 326, in refresh
    key = cPickle.load(open(key_pkl, 'rb'))
  File "&lt;anonymized&gt;/ops/TiledConvV3D.py", line 504, in &lt;module&gt;
    import TiledConvG3D
  File "&lt;anonymized&gt;/ops/TiledConvG3D.py", line 565, in &lt;module&gt;
    import TiledConvC3D
  File "&lt;anonymized&gt;/ops/TiledConvC3D.py", line 22, in &lt;module&gt;
    pdb.traceback.print_stack()
  </code></pre>
  
  <p>Moreover, I also check the id of <code>__builtin__.__import__</code> . At the very start of my main script, I import <code>__builtin__</code> and print <code>id(__builtin__.__import__)</code> before doing any other imports. I also print <code>id(__builtin__.import__)</code> from inside my module that is being imported multiple times, and the value of the id does not change.</p>
  
  <p>Are there other mechanisms besides calling reload and overriding <code>__builtin__.__import__</code> that could explain my module getting loaded multiple times?</p>