mtmt
mtmt

Reputation: 173

from __future__ import absolute_import not working? sub-modules not visible

So I've got a module bbb in the main scope as well as ccc.

I'm adding a library called tools which also has 2 modules called bbb and ccc:

tools

  • __init__.py
  • aaa.py
  • bbb.py
  • ccc.py

In bbb.py I'm importing the main scope bbb with:

from __future__ import absolute_import
import bbb

and in ccc.py doing the same thing:

from __future__ import absolute_import
import ccc

but when I import tools and dir it I can only see:

['__builtins__', '__doc__', '__file__',
'__name__', '__package__', '__path__', 'aaa']

but the bbb and ccc don't seem to be visible.

Am I missing something here?

Upvotes: 1

Views: 1637

Answers (2)

user2357112
user2357112

Reputation: 280887

but when I import tools and dir it I can only see:

['__builtins__', '__doc__', '__file__',
'__name__', '__package__', '__path__', 'aaa']

but the bbb and ccc don't seem to be visible.

Importing a package doesn't automatically load all its submodules. If you want to use the tools.bbb package, you need to do

import tools.bbb
# or
from tools import bbb

import tools won't cut it. Alternatively, you can have tools explicitly load its submodules in its __init__.py:

# in __init__.py
from . import aaa, bbb, ccc

Upvotes: 4

Joe
Joe

Reputation: 2564

Use the dot notation:

From bbb.py, if you want to import aaa.py:

from . import aaa

From outside tools, if you want to import tools/aaa.py:

from tools import aaa

Upvotes: 1

Related Questions