-2

So I have a file structure resembling this

src
|--- app
   |--- __init__.py
   |--- calc.py
|--- assets
|--- main.py
|--- environment.yml

In __init__.py I just have

print('Importing app...")

and calc.py has

from tkinter import Tk, Frame, Label

class App(Tk):
   def __init__():
      super().__init__()
      Label(self, text='Hello, World!').pack()

and finally in main.py

import app

print(dir(app))

However, app only has

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

Where is my App class?

1 Answers1

0

In __init__.py I just have

print('Importing app...")

So this code will be executed when you import app. Thus, app won't have an attribute App from calc.py because the App class isn't mentioned in __init__.py.

If you change __init__.py to look like this:

print('Importing app...")

from .calc import App

...then import app; app.App will work.


Example

$ mkdir pymod # setup module structure
$ cd pymod
$ echo "class App: ..." > calc.py
$ echo "from .calc import App" > __init__.py
$ cd .. # get out of the module
$ echo "import pymod; print(dir(pymod))" > test.py # use module
$ python3 test.py # run code
['App', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'calc']

As you can see, App is now present.

ForceBru
  • 43,482
  • 10
  • 63
  • 98