Importing a submodule of a custom package using the dot notation?

1k Views Asked by At

I'm guessing this may already be answered, but all my searches are turning up other import issues. Then again, perhaps I just don't know the proper terms to search for.

If I create a package with a module, it seems I can use from mypackage import mymodule to use mymodule in my code. However, I can't use import mypackage and then use mypackage.mymodule. With this I get a AttributeError: 'module' object has no attribute 'mymodule' error. Why is this and how do I set it up so I can use this?

My package structure is shown below. All the __init__.pys are empty.

myproject
  __init__.py
  mypackage
    __init__.py
    mymodule.py
  mymain.py # Doing the importing.
1

There are 1 best solutions below

0
On BEST ANSWER

Submodules are not implicitly imported. You will need to import .mymodule in mypackage/__init__.py unless you explicitly want to import mypackage.mymodule.