Importing modules implicit vs explicit

Viewed 1844

I want to import the module foo and its submodule foo.bar. I can do that as follows:

import foo.bar

Now both foo and foo.bar are available as I want. The docs state this is due to foo being imported first:

In this case, Python first tries to import foo, then foo.bar, and finally foo.bar.baz.

It wasn't clear to me that this was the way imports worked. Now that I know the following seems rather stupid:

import foo
import foo.bar

Is the single import clear enough for other programmers?

1 Answers

The single import is not clear enough to run the program nicely. We need to specify clear instructions that from import packages what items we want to use.

You can also write it in this way,

import  foo
from foo import foo.bar
from foo import foo.bar.baz
Related