Jack_of_All_Trades
Jack_of_All_Trades

Reputation: 11468

Multiple imports of python modules

Just for my knowledge, how does python, especially wxpython reacts to multiple imports? If I import wx in multiple files, how does it handle that when called the main frame? Does it slows the speed or it firstly checks whether it is already been imported or not?

Upvotes: 2

Views: 121

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

Only the first import executes the file. Subsequent imports copy the reference from sys.modules.

Upvotes: 3

Ethan Furman
Ethan Furman

Reputation: 69051

When Python imports a file, it keps track of it by storing it in sys.modules. So whenever Python is importing a file it checks there first and, if it finds it there, returns that instead; if it is not there, it imports it, adds it to sys.modules, and then returns it.

Upvotes: 5

Related Questions