Importing modules from subpackage

Viewed 57

My project has this kind of structure:

mypackage
|
|-- __init__.py
|
|-- file.py
|
|-- subpackage
    |
    |-- __init__.py
    |
    |-- function.py

How do I import the modules in the subpackage from file.py?

I tried a couple of things like simply import subpackage or from subpackage import function but all of them lead to ModuleNotFoundError: No module named 'subpackage'.

from . import subpackage has no error message but I don't know how to access the module with that.

2 Answers

I solved it by using:

from mypackage.subpackage import function 

Just use

from subpackage import function

You propably are not in mypackage directory

Related