1

I am learning Python and while studying modules two doubts came to my mind.

  • Doubt #1:

    I know that module is just simply python file such as filename.py, but what is a sub-module in Python?

  • Doubt #2:

    Consider the following three lines of code:

    import modulename
    from pkgname import modulename
    from pkgname import *
    

    Do all three statements use __init__.py (which is inside pkgname package) or only the third statement?

3
  • 1
    1. A submodule is a module inside a package. 2. pkgname/__init__.py is used whenever you import pkgname or one of its submodules (that is, lines 2 and 3 of your example). Commented Aug 22, 2015 at 9:09
  • how a package can consists sub-module, i mean as i know a package can consists of either module or sub-package(dimply directory). but how sub-module ? please give me any example. Commented Aug 22, 2015 at 9:31
  • As the package itself is also a module (content defined in __init__.py) any module in that package can be considered a sub-module of that package/module. Commented Aug 22, 2015 at 14:46

2 Answers 2

4

To make things a bit more clearer about your second question.


When you do -

import packagename.modulename

or

from packagename import modulename

Python internally first imports packagename , and when I say python imports packagename , I mean it imports the __init__.py of that package , and then after that it imports modulename . This is the reason why when you do any of the above it imports the __init__.py .


When you do -

from packagename import *

Please note, this does not import modulename from packagename by default , this would only import the __init__.py from packagename , and all modules that you have listed in __all__ list in __init__.py , if no modules are listed in that list, none would be imported. Example -

Lets say I have

shared
   -- __init__.py
   -- a.py

__init__.py looks like -

print("In Shared")

a.py look like -

print("In A")

Now when in the directory above shared ,and openning python, if you do -

from shared import *

It would print out -

In Shared

But if you change that code in __init__.py to -

print("In Shared")
__all__ = ['a']

And do the same import from same location, It would print out -

In Shared
In A

As you can see it only imports submodules that are defined in the __all__ list.


Finally , when you do -

import modulename

Lets say you do that directly from within packagename , by changing the directory to it and openning python interactive interpreter there. At that time, you are not asking Python to import packagename for you, so it does not need to import packagename , and hence it does not import __init__.py .

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome reply dude, you always help me in the best possible way. Thanku dude once again.
Glad I could be helpful :)
No, I mean click on the tick mark , tick mark is for accepting the answers , up-button is for upvotes.
3

As Andrea Corbellini says in the comment:

  1. A submodule is a module inside a package. A Python package is simply a directory of Python module(s) that contains an __init__.py file.
  2. pkgname/__init__.py is used whenever you import pkgname or one of its submodules (that is, lines 2 and 3 of your example). Your first example doesn't import any package or module of a package therefore it doesn't need to use __init__.py (Which can be empty)

If there are these files, and you are working on the same folder

./pkgname/__init__.py  # Submodule of pkgname package used on example 3) to import all the submodules
./pkgname/Asdf.py  # Submodule of pkgname package
./pkgname/modulename.py  # Submodule of pkgname package imported in example 2) using __init__.py
./modulname.py  # Module imported on example 1)

3 Comments

Thanku for replying. My first doubt have got cleared. But in case of my second doubt, as u said Your first example doesn't import any package or module of a package therefore it doesn't need to use init.py (Which can be empty), but i think it is importing module(i.e. sub-module) then why it is not using init.py(as you said) ?
in case of ./modulname.py # Module imported on example 1) there would be a module called ./init.p. While importing ./modulname.py using import import modulename statement will it use ./init.p ?
No, you don't need __init__.py to import ./modulname.py. Check it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.