I have a module file called mymodule.py, which contains the following code:
class foo:
def __init__(self):
self.foo = 1
class bar:
import foo
def __init__(self):
self.bar = foo().foo
The __init__.py file in the same directory has
from mymodule import foo
From a script in the same directory, I have the following code:
from mymodule import bar
When I try to run bar(), I get the error that No module named foo. How can I create an instance of foo in bar when they are defined within the same module file?
Classes are imported with module name first. However, you don’t need to import classes in mymodule from within mymodule, just use it.
Meaning: remove the import foo line