Monday, May 25, 2020

No reimporting of module in Python script

Chances are you have been working with imported modules in python. And you usually import the modules from the very top of the script file.

What happen when you import same module twice or more?

The simple answer in nothing! In python when you import a module and you try to import it again in the same instance of a script it won't work!

That is why when you work with modules such as these listed below, it is easy to get confused that the modules work only the first time you ran them.

  • import __hello__
  • import this
  • import antigravity

You will only see the result of their import once.


import __hello__: will print the famous "Hello World!" as an output.




import this: will print the 'Zen of Python'




import antigravity: will open a browser link to python antigravity xkcdwebcomic.




If you try to run any of the above for the second time, none will work.


Obviouly, I don't see any reason why you will reimport a module. But if you do it for whatever reason, then the second import won't work.

No comments:

Post a Comment