'How to import module inside sub-package of a package in python without knowing the sub-package name
I have my python directory structure something like the following:
Directory Layout
data
|
+-- __init__.py (Empty)
|
|
+-- foo.py
| |
| +-- __init__.py (Empty)
| |
| +-- a.py
| |
| +-- b.py
|
|
+-- bar.py
|
+-- __init__.py (Empty)
|
+-- c.py
|
+-- d.py
To import modules
a, b, c & d
I am trying to use importlib.import_module to import these modules. e.g.
mod_a = importlib.import_module("data.foo.a")
mod_b = importlib.import_module("data.foo.b")
mod_c = importlib.import_module("data.bar.c")
mod_d = importlib.import_module("data.bar.d")
Is there a way by which I can import the a, b, c or d module directly without knowing the name of sub-package in which they are present?
(Maybe modifying any of the __init__.py files ??)
That is, I want to do something like this:
mod_a = importlib.import_module("data.a")
mod_b = importlib.import_module("data.b")
mod_c = importlib.import_module("data.c")
mod_d = importlib.import_module("data.d")
P.S.: The names a,b,c & d are unique
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
