Shonu93 Shonu93 - 1 year ago 47
Python Question

what happens when i import module twice in python

I have a doubt that I would like to get cleared.

Consider the following module named

print("Hello, I'm ex_1")
def greet(name):
print("Hello, "+name+" nice to meet you! ")

Now consider another file called
that will import


Now when I execute this file, I get the output as :

Hello, I'm ex_1

As expected.

But when I change to


and execute it, it still prints only
Hello, I'm ex_1
once. Shouldn't it print it twice ?


Nothing, if a module has already been imported, it's not loaded again.

You will simply get a reference to the module that has already been imported (it will come from sys.modules).

To get a list of the modules that have already been imported, you can look up sys.modules.keys() (note that urllibhere imports a lot of other modules):

>>> import sys
>>> print len(sys.modules.keys())
>>> print sys.modules.keys()
['copy_reg', 'sre_compile', '_sre', 'encodings', 'site', '__builtin__', 'sysconfig', '__main__', 'encodings.encodings', 'abc', 'posixpath', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'types', '_codecs', 'encodings.__builtin__', '_warnings', 'genericpath', 'stat', 'zipimport', '_sysconfigdata', 'warnings', 'UserDict', 'encodings.utf_8', 'sys', 'virtualenvwrapper', '_osx_support', 'codecs', 'readline', 'os.path', 'sitecustomize', 'signal', 'traceback', 'linecache', 'posix', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
>>> import urllib
>>> print len(sys.modules.keys())
>>> print sys.modules.keys()
['cStringIO', 'heapq', 'base64', 'copy_reg', 'sre_compile', '_collections', '_sre', 'functools', 'encodings', 'site', '__builtin__', 'sysconfig', 'thread', '_ssl', '__main__', 'operator', 'encodings.encodings', '_heapq', 'abc', 'posixpath', '_weakrefset', 'errno', '_socket', 'binascii', 'encodings.codecs', 'urllib', 'sre_constants', 're', '_abcoll', 'collections', 'types', '_codecs', 'encodings.__builtin__', '_struct', '_warnings', '_scproxy', 'genericpath', 'stat', 'zipimport', '_sysconfigdata', 'string', 'warnings', 'UserDict', 'struct', 'encodings.utf_8', 'textwrap', 'sys', 'ssl', 'virtualenvwrapper', '_osx_support', 'codecs', 'readline', 'os.path', 'strop', '_functools', 'sitecustomize', 'socket', 'keyword', 'signal', 'traceback', 'urlparse', 'linecache', 'itertools', 'posix', 'encodings.aliases', 'time', 'exceptions', 'sre_parse', 'os', '_weakref']
>>> import urllib #again!
>>> print len(sys.modules.keys()) #has not loaded any additional modules

Let's give it a whirl:

import sys
>>> sys.modules["foo"] = "bar"  # Let's pretend we imported a module named "foo", which is a string.
>>> print __import__("foo")
bar  # Not a module, that's my string!

As you can see, if a module is found un sys.modules, you'll just get a new reference to it. That's it.

Note that this means that modules should be designed so as to not have side effects (such as printing stuff) when they're imported.

Reloading modules, outside of an interactive session, is usually not a very good practice either (although it has its use cases) - the other answers will detail how you'd do this.