I have a package whose structure is like this:
/backends/
__init__.py
abc.py
def.py
ghi.py
xyz.py
common.py
The modules abc.py
, def.py
, ghi.py
and xyz.py
contain some common functions e.g. func0()
and func1()
.
In the common.py
module I am importing * from all modules like this:
from abc import *
from def import *
from ghi import *
from xyz import *
I don't think this is a Pythonic way to do this. Think about a few tens of such modules.
What I want is to have a single line statement which imports * from all the modules in the package. Something like this:
from backends import *
I tried this link, but couldn't get what I wanted. I created a variable __all__
in the __init__.py
and assigned the list of all modules to it. The I put this import line in the common.py
module:
from . import *
And then I tried to access the function func0()
which is present in any module (except in __init__.py
and common.py
) in the package. But it raised an error which reads
ValueError: Attempted relative import in non-package
I need a detailed answer.