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?
What happens if a module is imported twice? The module is only loaded the first time the import statement is executed and there is no performance loss by importing it again. You can examine sys. modules to find out which modules have already been loaded.
Yes, you can import a class twice in Java, it doesn't create any issues but, irrespective of the number of times you import, JVM loads the class only once.
Yes, you can import module as many times as you want in one Python program, no matter what module it is. Every subsequent import after the first accesses the cached module instead of re-evaluating it. And you can see the cached modules in sys.
For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it's just one module you want to test interactively, use importlib.
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 urllib
here 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.
A Python script will load a module only once. To reload it, use:
reload() # Python 2
and
imp.reload() # Python 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With