python ImportError without import call
Question: Okay, I'm stumped on this one. I've looked around but I can't find anything
and can't figure out how to debug this. Basically, python is throwing an
`ImportError` at a line of code where I'm not importing anything. I've a
decently large module `ICgen` which contains the module `ICgen_settings`.

    
    
    Traceback (most recent call last):
    
    File "<ipython-input-5-105e3826f255>", line 1, in <module>
    IC = ICgen.load('IC.p')
    
    File "diskpy/ICgen/ICgen.py", line 339, in load
    ICobj.settings.load(input_dict['settings'])
    
    File "diskpy/ICgen/ICgen_settings.py", line 484, in load
    tmp_dict = pickle.load(open(settings_filename, 'rb'))
    
    ImportError: No module named ICgen_settings
    

This doesn't make any sense to me. It clearly has found `ICgen_settings` since
it's throwing the error from within it. Furthermore, I'm not doing an `import`
call when it throws the error!

Any ideas?


Answer: When you try to `pickle.load` the `tmp_dict`, the modules for any of the
objects in the incoming data stream need to be loaded.

So yes, you were in fact doing an `import` call when the error was thrown: you
were unpickling an object of some type that needed `ICgen_settings`. NB:
unpickling code can run arbitrary Python statements. Never unpickle objects
you don't trust!

Now, as to why it's "clearly found `ICgen_settings`": No, just being in a file
called `ICgen_settings.py` does not mean that the line `import ICgen_settings`
will succeed. Whether the import succeeds depends on `sys.path`, which
originates from your `PYTHONPATH` environment variable. It also depends on the
module layout of `ICgen_settings`: normally, it would be an `ICgen_settings`
**folder** (not file) containing a `__init__.py` file.

