What happens when I import a module twice in python

I have doubts that I want to be cleaned.

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 the ex_1.py module.

 import ex_1.py 

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

 Hello, I'm ex_1 

As expected.

But when I go to 1_client_ex_1.py in:

 import ex_1.py import ex_1.py 

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

+35
python module
Sep 29 '13 at 11:12
source share
3 answers

Nothing, if the module is already imported, it does not load again.

You simply get a link to a module that has already been imported (it will be obtained from sys.modules ).

To get a list of already imported modules, you can find sys.modules.keys() (note that urllib imports a lot of other modules here):

 >>> 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 

Give him a whirlwind:

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

As you can see, if the module is not found by sys.modules , you just get a new link to it. What is it.




Please note that this means that the modules must be designed so as not to have any side effects (such as printed materials) when they are imported .

Rebooting modules outside an interactive session is usually not a very good practice (although it does have its own use cases). Other answers detail how you do it.

+46
Sep 29 '13 at 11:14
source share

Any python script will reload the module only once. To reload it, use:

 reload() # Python 2 

and

 imp.reload() # Python 3 
+5
Sep 29 '13 at 11:23
source share

A module is loaded only the first time the import statement is executed. See Also reload() and this question . You can examine sys.modules to find out which modules are already loaded.

+3
Sep 29 '13 at 11:17
source share



All Articles