user6654733 user6654733 - 4 months ago 30
Python Question

result when __all__ is not defined in __init__.py of package?

I am just learning and practicing python,on the way,i am reading about python packages and how to import into other modules or package at Modules ,I assume the following scenario ,

I have package as ,

Video/
__init__.py
formats/
__init__.py
mkv.py
mp4.py
length/
__init__.py
morethan20min.py
lessthan20min.py


and in no

__init__.py


I have not defined

__all__


what happens if i have an import statement as,

import Video.format.mkv
import Video.formats.*


Since I have already imported mkv module in first statement,what exactly happens after execution of second import statement,I didnt get the concept after reading on the mentioned link.

Answer

When you do

from whatever_package import *

first, if the package's __init__.py hasn't been run yet, it will be run. (If you've already done import whatever_package.something_specific, the package's __init__.py will have already been run.)

Then, if whatever_package.__init__ does not define an __all__ list, the import will pick up all current contents of the whatever_package object. That'll be anything defined in __init__.py and any submodules that have already been explicitly imported by any code that has executed in your program. For example, if whatever_package's __init__.py is empty, you do

import whatever_package.something_specific
from whatever_package import *
import whatever_package.other_thing

and no other import statements relating to whatever_package exist in your program, then the import * will pick up something_specific, but not any other submodules of whatever_package, such as other_thing.