folder structure:
<current dir>
main.py
packages <dir>
__init__.py
mod.py
main py:
import packages
print packages.mod.hello()
mod.py:
def hello():
return 'hello'
__init__.py:
from packages import mod
If I run main.py
, I get no error. But if I edit __init__.py
to 'from packages import *'
, I get this error: AttributeError: 'module' object has no attribute 'mod'
I'm not asking how to make that 'print'
command work. I can use other 'import'
syntax in main.py
to make it work. The question is: I'm curious about that 'from packages import mod'
in the __init__.py
. If i can do import mod
then when I replace to import *
, which means import everything, why do I get an error instead?
So what does the from packages import *
really mean inside that __init__.py
?
Anyone can help? Thanks
Short answer
So what does the
from packages import *
really mean inside that__init__.py
?The
__init__.py
imports itself.Explanation
You can only import modules, not packages. Packages are just containers for modules or sub-packages. When you "import" a package you actually import the module
__init__.py
.The
__init__.py
with this content:imports the module
mod
into__init__.py
. Therefore, it will be available in yourmain.py
viapackages.mod
(rememberpackages
is represented by__init__.py
).When you change the content of
__init__.py
to:You are importing the module
__init__.py
, the very same file you are in. This works (a second import just triggers a lookup insys.modules
) but won't give you the content ofmod
.This means, you can use:
but you cannot sensibly use this with an empty
__init__.py
:Because
package
is actually represented by the__init__.py
and there is nothing in it yet. You can check this (interactively or in file):In
__init__.py
you can write:and then in
main.py
:works. Because the function
hello()
is now in the global name space of the file__init__.py
.As mentioned in the answer by mozman, you can use
__all__
in__init__.py
to list the modules that should be imported iffrom packages import *
is used. This is designed for this case.The
__init__.py
has only this content:Now you can do this in
main.py
:If you extend your
__init__.py
:You can do this in
main.py
:But if you remove the
from packages import *
from__init__.py
:You will get an error:
because the
__all__
is only used for thefrom packages import *
case. Now we are back to the__init__.py
imports itself.See also: In Python, what exactly does “import *” import?
adding __all__ to packages.__init__:
and module 'mod' will be imported, else 'mod' is not in the namespace of 'packages', but I can not explain why 'import *' without __all__ do not import 'mod'.
you can load the modules inside the same packages directly. The following code works and it loads all the modules inside mod.py.
Inside __init__.py
Efficient import - loads only hello function
In your code,
from packages import *
you are telling the interpreter to look for a modules insidepackages
(in the same directory as__init__.py
). But it does not exist there. It exist one directory above the__init__.py
. (I suspect my terminologies are wrong)Here is a reference that explains how to load the containing package itself.
FOUND IT
It was very interesting to read about python import mechanisms. Ref1 Ref2 Ref3
Apparently the parent modules is loaded first. For example, Ref3 states that, the code
import mod
inside__init__.py
is automatically interpreted aspackages.mod
. Now I have to find out what happens if you writeimport packages.mod
. Ref1 is more up-to-date with python3 conventions. Refer it for more info. Hope this helps you.