'how to make a directory as a package
I have 2 python files a and b that are mutually dependent which raises a circular import error anytime when I import one from the other. I saw this post online which talks about creating a third file __init.py__ in the same directory as a and b and putting all the imports in the third file, hence the folder containing these 3 files becomes a package, correct? So in this case, the package can be imported in both a and b and submodules can be accessed however when I import the package in a and b, it gives an error that package is not defined. I am not sure, if I am creating the package correctly. Below is my code demonstration.
/code
/package # this becomes a package, correct?
__init__.py
a.py
b.py
In __init__.py
from . import a
from . import b
In a.py
import package # this throws "package is not defined" error
package.b.some_variable
In b.py
import package # this also throws "package is not defined" error
package.a.some_variable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
