Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 250
Shiania White Avatar asked Oct 14 '25 15:10

Shiania White


1 Answers

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

like image 199
Ignacio Vazquez-Abrams Avatar answered Oct 17 '25 04:10

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!