14

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

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
andio
  • 1,574
  • 9
  • 26
  • 45

3 Answers3

22

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:

from packages import mod

imports the module mod into __init__.py. Therefore, it will be available in your main.py via packages.mod (remember packages is represented by __init__.py).

When you change the content of __init__.py to:

from packages import *

You are importing the module __init__.py, the very same file you are in. This works (a second import just triggers a lookup in sys.modules) but won't give you the content of mod.

This means, you can use:

from module import *

but you cannot sensibly use this with an empty __init__.py:

from package import *

Because package is actually represented by the __init__.py and there is nothing in it yet. You can check this (interactively or in file):

>>> import packages
>>> print(packages)
<module 'packages' from '/.../packages/__init__.py'>

In __init__.py you can write:

from packages.mod import *

and then in main.py:

print packages.hello()

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 if from packages import * is used. This is designed for this case.

The __init__.py has only this content:

__all__ = ['mod']

Now you can do this in main.py:

from packages import *

print mod.hello()

If you extend your __init__.py:

__all__ = ['mod']

from packages import *

You can do this in main.py:

import packages

print packages.mod.hello()

But if you remove the from packages import * from __init__.py:

__all__ = ['mod'] 

You will get an error:

AttributeError: 'module' object has no attribute 'mod'

because the __all__ is only used for the from packages import * case. Now we are back to the __init__.py imports itself.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • i **can** do `from package import *` since there will be no error when compiled, only if i print then it said no module 'mod'. So that command is valid , only i'm confused about what the code is doing . – andio Apr 09 '16 at 10:13
  • “You can only import modules, not packages,” suggests that packages and modules are mutually exclusive. However, per The Python Language Reference, [§5.2 Packages](https://docs.python.org/3/reference/import.html#packages): “[A]ll packages are modules, but not all modules are packages. … [P]ackages are just a special kind of module.” Further [a package can be imported](https://docs.python.org/3/reference/import.html#regular-packages): “When a regular package is imported, this `__init__.py` file is implicitly executed, and the objects it defines are bound to names in the package’s namespace.” – Jim Ratliff May 06 '22 at 16:23
  • See *When you "import" a package you actually import the module `__init__.py.`* in answer. This is essentially what you say. Not? – Mike Müller May 19 '22 at 07:30
1

See also: In Python, what exactly does “import *” import?

adding __all__ to packages.__init__:

__all__ = ['mod']
from packages import *

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'.

Community
  • 1
  • 1
mozman
  • 2,001
  • 8
  • 23
  • i'll have a look the link and research it. i'll be back when i got something. – andio Apr 09 '16 at 10:35
  • Thanks but something is still missing, here what i got... if __all__ is defined then it should import all the names defined in this sequence, otherwise it will import all names, except these which start with one underscore. so in my case if i didn't define __all__ , it should import all names including my 'mod' , right ? – andio Apr 09 '16 at 13:46
0

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

from mod import *
print hello()

Efficient import - loads only hello function

from mod import hello
print hello()

In your code, from packages import * you are telling the interpreter to look for a modules inside packages(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 as packages.mod. Now I have to find out what happens if you write import packages.mod. Ref1 is more up-to-date with python3 conventions. Refer it for more info. Hope this helps you.

Community
  • 1
  • 1
Mitty
  • 330
  • 2
  • 15
  • Thanks but that's not my question , i can do other import syntax to make that print works. But I'm asking about that import * which should import everything including ** mod ** – andio Apr 09 '16 at 09:57
  • also you said the module named packages does not exist, but in fact if i did this : from packages import mod. it works ! try to delete this code and run it, it will give you error. but when i put 'from packages import mod' , it works. So it means it's there. I know what you mean, that the packages is upper level, that why i'm confused . it should be 'illegal' , but the 'from packages import mod' works ! that's why i'm confused. i may change my question a bit ... why then the 'from packages import mod' work? – andio Apr 09 '16 at 10:19
  • Okay that's fascinating. Can you specify the platform OS and python version, so i can recreate the scenario and test it. I will check it. – Mitty Apr 09 '16 at 10:23
  • @andio added some references, I still have to recreate your scenario. – Mitty Apr 09 '16 at 10:54
  • @andio I tested the same code inside an IDE while specifying `packages` as the root folder. Now `from packages import mod` and `from packages.mod import *` both works. But running the `_init__.py` inside a terminal throws `ImportError: No module named 'packages' `. This is already discussed in the Ref 1,2 and 3 in detail. The packages is not known before the import is invoked. So when we try to load from packages it says `ImportError`. – Mitty Apr 09 '16 at 11:10