Forums

Multi page Dash app: AttributeError: module '__main__' has no attribute '__file__'

I can't seem to get my app up and running. Whenever I start the server it crashes because the _pages.py file of dash finds a module without a __file__ attribute.

It might be worth noting that I do not make the dash.Dash instance in my app.py. Instead I import this instance from a init_app.py file in my "callbacks" folder.

Any insight into the matter would be greatly appreciated. Hereby the error.log:

2023-07-09 14:51:13,574: Error running WSGI application
2023-07-09 14:51:13,575: AttributeError: module '__main__' has no attribute '__file__'
2023-07-09 14:51:13,575:   File "/var/www/birdies_pythonanywhere_com_wsgi.py", line 16, in <module>
2023-07-09 14:51:13,575:     from app import app
2023-07-09 14:51:13,575: 
2023-07-09 14:51:13,576:   File "/home/birdies/mysite/app.py", line 5, in <module>
2023-07-09 14:51:13,576:     from callbacks import app
2023-07-09 14:51:13,576: 
2023-07-09 14:51:13,576:   File "/home/birdies/mysite/callbacks/__init__.py", line 1, in <module>
2023-07-09 14:51:13,576:     from .navbar import app
2023-07-09 14:51:13,577: 
2023-07-09 14:51:13,577:   File "/home/birdies/mysite/callbacks/navbar.py", line 2, in <module>
2023-07-09 14:51:13,577:     from .init_app import app
2023-07-09 14:51:13,577: 
2023-07-09 14:51:13,577:   File "/home/birdies/mysite/callbacks/init_app.py", line 4, in <module>
2023-07-09 14:51:13,578:     app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP], pages_folder="/home/birdies/mysite/pages/")
2023-07-09 14:51:13,578: 
2023-07-09 14:51:13,578:   File "/home/birdies/.local/lib/python3.10/site-packages/dash/dash.py", line 511, in __init__
2023-07-09 14:51:13,578:     self.init_app()
2023-07-09 14:51:13,578: 
2023-07-09 14:51:13,579:   File "/home/birdies/.local/lib/python3.10/site-packages/dash/dash.py", line 589, in init_app
2023-07-09 14:51:13,579:     self.enable_pages()
2023-07-09 14:51:13,579: 
2023-07-09 14:51:13,579:   File "/home/birdies/.local/lib/python3.10/site-packages/dash/dash.py", line 2012, in enable_pages
2023-07-09 14:51:13,579:     _import_layouts_from_pages(self.config.pages_folder)
2023-07-09 14:51:13,579: 
2023-07-09 14:51:13,580:   File "/home/birdies/.local/lib/python3.10/site-packages/dash/_pages.py", line 432, in _import_layouts_from_pages
2023-07-09 14:51:13,580:     module_name = _infer_module_name(page_path)
2023-07-09 14:51:13,580: 
2023-07-09 14:51:13,580:   File "/home/birdies/.local/lib/python3.10/site-packages/dash/_pages.py", line 109, in _infer_module_name
2023-07-09 14:51:13,580:     if _module_name_is_package(CONFIG.name):
2023-07-09 14:51:13,580: 
2023-07-09 14:51:13,581:   File "/home/birdies/.local/lib/python3.10/site-packages/dash/_pages.py", line 90, in _module_name_is_package
2023-07-09 14:51:13,581:     and Path(sys.modules[module_name].__file__).name == "__init__.py"
2023-07-09 14:51:13,581: ***************************************************
2023-07-09 14:51:13,581: If you're seeing an import error and don't know why,
2023-07-09 14:51:13,581: we have a dedicated help page to help you debug: 
2023-07-09 14:51:13,581: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-07-09 14:51:13,582: ***************************************************

Where are you running your app?

Do you mean the url? Its birdies.pythonanywhere.com

I think that @nkahr was asking about how you'd set up the WSGI file to load the app, but I think that might be a red herring.

On the last error line, it looks like it's trying to check if a module is a package, and on the one above, it looks like it's looking at CONFIG.name. Do you set the Dash app's name in your config? If so, what do you set it to?

Thank you very much! I wasn't initializing the app to __name__. The error is no longer popping up now!