0

I have trouble importing a package thats import a modul.

I have a package thats get imported in my project. In that package i import a module.

The real problem is an uwsgi flask app, but thes example illustrates my problem. How can I import a moduel so i works if i run init.py og lev0.py

Structure

project
  - subfolder
     - __init__
     - lev2.py
  - lev0.py

lev2.py Runs an prints as expected

class Lev2:
    def __init__(self):
        self.name = "lev2"


if __name__ == "__main__":
    obj2 = Lev2()
    print(obj2.name)

init.py Runs and print as expected

from lev2 import Lev2

class Lev1:
    def __init__(self):
        self.name = "lev1"
        self.child = Lev2()

if __name__ == "__main__":
    obj1 = Lev1()
    obj2 = Lev2()
    print(obj1.name, obj1.child.name)

lev0.py

Error: No module named lev2

import subfolder

class Lev0:
    def __init__(self):
        self.name = "lev0"
        self.child = subfolder.Lev1()

if __name__ == "__main__":
    obj0 = Lev0()
    obj1 = subfolder.Lev1()
    print(obj1.name, obj1.child.name)
  • Possible duplicate? [how to import classes defined in ___init___.py files](https://stackoverflow.com/questions/582723/how-to-import-classes-defined-in-init-py) – M.K Nov 29 '22 at 17:10
  • Does this answer your question? [How to import classes defined in \_\_init\_\_.py](https://stackoverflow.com/questions/582723/how-to-import-classes-defined-in-init-py) – M.K Nov 29 '22 at 17:11

0 Answers0