Hi.
This behaviour is causing me troubles when I want to launch the app with python -m
and I have to do relative imports in there.
So, Let’s say I want to run the dash app as a module using the python -m
flag. Inside the __main__.py
, I import the factory function to create the dash instance I want to launch, first time the __main__
file runs it works correctly, but with the second round that the debugger mode triggers it fails. I’ve been messing around and this is because the sys.path changes from the initial, normal, launch; to the debugger subprocess launch.
This is my wikichron/__main__.py
code (summarized for clarity):
# External imports
import os
from urllib.parse import urljoin, parse_qs
import pandas as pd
import sys
print(f'This is the sys.path:{sys.path}')
# flask imports
import flask
# local imports
from .app import create_dash_app, set_up_app # ¡WITH SECOND LAUNCH, IT FAILS HERE!
# load config
from .dash_config import DevelopmentConfig
wikichron_base_pathname = DevelopmentConfig.DASH_BASE_PATHNAME;
port = DevelopmentConfig.PORT;
print(f'Starting execution of __main__')
global debug;
debug = True if os.environ.get('FLASK_ENV') == 'development' else False
def run(app):
print('Start running the app standalone...')
app.run_server(debug=debug, port=port)
return
server = flask.Flask(__name__)
# load config for flask server
server.config.from_object(DevelopmentConfig)
# create and config Dash instance
app = create_dash_app(server)
# set layout, import startup js and bind callbacks
set_up_app(app)
run(app)
If I launched the app with python3 -m wikichron/, I’d get this:
Generating available metrics...
=> You are in DEBUG MODE <=
['', '/home/akronix/.pyenv/versions/3.6.7/lib/python36.zip', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6/lib-dynload', '/home/akronix/.pyenv/versions/wikichron/lib/python3.6/site-packages']
FOOOOO
Creating new Dash instance...
Binding callbacks...
Setting up layout...
¡¡¡¡ Welcome to WikiChron 2.0.0-alpha !!!!
Using version 0.39.0 of Dash.
Using version 0.20.0 of Dash renderer.
Using version 0.44.0 of Dash Core Components.
Using version 0.3.4 of Grasia Dash Components.
Using version 0.14.0 of Dash Html Components.
Start running the app standalone...
Running on http://127.0.0.1:8880/app/
Debugger PIN: 040-845-928
* Serving Flask app "__main__" (lazy loading)
* Environment: development
* Debug mode: on
['/home/akronix/workspace/wikis_research/WikiChron/wikichron/dash/apps/classic', '/home/akronix/.pyenv/versions/3.6.7/lib/python36.zip', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6/lib-dynload', '/home/akronix/.pyenv/versions/wikichron/lib/python3.6/site-packages']
Traceback (most recent call last):
File "/home/akronix/workspace/wikis_research/WikiChron/wikichron/dash/apps/classic/__main__.py", line 13, in <module>
from .app import create_dash_app, set_up_app
ModuleNotFoundError: No module named '__main__.app'; '__main__' is not a package
As you can see, the path is correct for the first launch, but incorrect for the second launch thus it fails. This is the source of the problem. Is there any way to pass the current path to the process which monitor the dash process?
If I run not in debug mode, the app launches successfull, as expected.
You can see the full code here.