On Sun, Feb 28, 2016 at 11:40 PM, <alien2u...@gmail.com> wrote: > Hello list, > > We can not import a module twice in a session of Python (subsequent attempts > to import same module don't result in any error though, but it is > not-effective). > > However after making change to module, we can reload() it (if not reload(), > we could possibly have reimport statement) to get the updates in module. > > I am a newbie in Python (learning via IDLE) and trying to understand > > - what is extra (special) that import does, that reload() doesn't?
When you import a module, it's cached in the sys.modules dict. Subsequent imports of the same module find the cached module rather than reimport it. However, reload() ignores the cache and always reloads the module. > - if subsequent imports of same module in a session are not effective, why > not simply flag those attempts as an error, rather than letting them go > effect-less. Because there are legitimate reasons for importing the same module multiple times. For example, I have a script that imports the sys module. It also imports the os module, which imports the sys module. Both the main module of my script and the os module have need of the sys module, so they both import it. If subsequent imports of the module raised an exception, this wouldn't be possible. However, they shouldn't get two separate copies of the sys module, because that would be wasteful and potentially error-prone if the module is stateful In the case of the sys module, there is state -- the sys.modules cache that I mentioned earlier. Imagine the confusion if two separate imports of the sys module resulted in two separate instances with two separate caches of imported modules that were in disagreement. -- https://mail.python.org/mailman/listinfo/python-list