Module import question

Bob Gailer bgailer at gmail.com
Sun Aug 9 14:04:22 EDT 2020


On Aug 9, 2020 11:41 AM, "Mats Wichmann" <mats at python.org> wrote:
>
> On 8/9/20 12:51 AM, Gabor Urban wrote:
> > Hi guys,
> >
> > I have a quite simple question but I could not find the correct answer.
> >
> > I have twoo modules A and B. A imports B. If I import A in a script,
Will
> > be B imported automatically? I guess not, but fő not know exactly.
> >
> > Thanks for your answer ín advance,
>
> Think of import as meaning "make available in namespace".

Well it's actually a little more involved than that. When python import a
module it executes the module code. This is why you often see at the bottom
of a module:

if __name__ == '__main__':
  # code to execute when running the module as opposed to importing it.

When importing a module __name__ is the module's name rather than
'__main__'.

What happens when module A Imports module B depends on whether or not the
import B statement is actually executed.


More information about the Python-list mailing list