BendEg
BendEg

Reputation: 21088

Does Python import all modules in a module path?

Let's assume I have the following structure.

main.py
    /mod1
        __init__.py
        mod1.py
        /mod2
            __init__.py
            mod2.py

And I have the following line in main.py.

import mod1.mod2

In this case does mod1 also get imported?

Upvotes: 2

Views: 56

Answers (2)

Ozan
Ozan

Reputation: 1074

Yes; mod1 is imported as well, and you can access mod1 solely as mod1 within your code if you do not write an alias like this import mod1.mod2 as mod2.

Python needs to import the modules consecutively so that it is able to import last module. You can test this by putting print statements in your __init__.py files

Upvotes: 2

Tom Zych
Tom Zych

Reputation: 13576

Yes. Try this in the interpreter:

import os.path
dir
os

As this shows, os is present in the main namespace.

Upvotes: 1

Related Questions