What happens when a module is imported twice?

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

Consider the following module named ex_1.py:

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

Now consider another file called 1_client_ex_1.py that will import ex_1.py module.

import ex_1.py

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

Hello, I'm ex_1

As expected.

But when I change to 1_client_ex_1.py to:

import ex_1.py
import ex_1.py

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


Solution 1:

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())
44
>>> 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())
70
>>> 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
70

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.

Solution 2:

A Python script will load a module only once. To reload it, use:

reload()      # Python 2

and

imp.reload()  # Python 3

Solution 3:

Module is only loaded the first time the import statement is executed. See also reload() and this question. You can examine sys.modules to find out which modules have already been loaded.