I have trouble importing a module in same directory. Basically, here is my organization
alex (folder)
|
|- alex (folder)
| |- __init__.py
| |- commons.py
| |- KnowledgeBase.py
|
|- test.py
in my init.py file, I have the following lines to make import of commons & KnowledgeBase easier in test.py. It appears to work.
from .KnowledgeBase import KnowledgeBase
from .commons import *
In my KnowledgeBase.py file, I have a class called KnowledgeBase.
In commons.py, I have different objects:
a namedtuple called CDE
an object called TimePeriod
And then, in test.py, I can call my objects this way.
import alex as al
do stuff with al.KnowledgeBase
do stuff with al.CDE
do stuff with al.TimePeriod
What doesn't work now.
I am calling in KnowledgeBase.py the module commons.py.
import alex as al
stuff with al.CDE ...
I get the following error message.
AttributeError: module 'alex' has no attribute 'CDE'
If I do it the traditional way:
import commons as AC
stuff with AC.CDE ...
I get the following error message.
ModuleNotFoundError: No module named 'commons'
And if I remove everything in the init.py file, it works, but then, I cannot use "easy module call" in test.py.
If I keep an empty init.py file, in test.py, instead of simply writing
import alex
I have to declare all imports:
from alex import KnowledgeBase
from alex import commons
Please, what can I do to keep the import of the different python modules in alex "simple" and at the same time, being able to import module from alex folder into other modules in alex folder?
I thank you in advance for your help. Have a good afternoon, Bests,
I come back after having found the appropriate formatting. In KnowledgeBase.py, the correct way to import functions or objects from commons is by typing:
from . import commons as AC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With