ashald / EnvFile

EnvFile 3.x is a plugin for JetBrains IDEs that allows you to set environment variables for your run configurations from one or multiple files.
MIT License
539 stars 129 forks source link

Not working with Flask configuration #96

Closed kmejka closed 4 years ago

kmejka commented 4 years ago

Hi, The plugin is awesome, but I'm having issues with a Flask project in PyCharm. Perhaps I'm doing something wrong. Any help appreciated. The issue is not that easy to explain in writing, so please bear with me, I'm trying my best :).

When I have a Run configuration of type 'Flask server' with environment variables, everything works fine. The application starts up like this (replaced every sensitive info with <...>):

FLASK_APP = root_app:app
FLASK_ENV = development
FLASK_DEBUG = 0
In folder <....>
<....>/venv/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --module --multiproc --qt-support=auto --client 127.0.0.1 --port 49766 --file flask run
pydev debugger: process 8527 is connecting

Connected to pydev debugger (build 192.6817.19)
<....>
Creating Elasticsearch connection to [localhost:9200]
secrets path now: <....>
Using fake credentials from <....>
WARNING:root:Using dummy credentials manager as DEV_MODE is set to `true`
Starting metrics client on `('127.0.0.1', 8125)`
Using <....>
<....>
Creating Elasticsearch connection to [localhost:9200]
 * Serving Flask app "root_app:app"
 * Forcing debug mode off
INFO:werkzeug: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Received request: GET http://localhost:5000/<....> (<....>).
Using mock <...>
INFO:werkzeug:127.0.0.1 - - [09/Oct/2019 16:51:15] "GET /<....> HTTP/1.1" 200 -

If I attach these same variables using an env file, then the application starts up like this:

FLASK_APP = root_app:app
FLASK_ENV = development
FLASK_DEBUG = 0
In folder <...>
<...>/venv/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --module --multiproc --qt-support=auto --client 127.0.0.1 --port 50043 --file flask run
pydev debugger: process 8665 is connecting

Connected to pydev debugger (build 192.6817.19)
 * Serving Flask app <something different>
 * Forcing debug mode on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
pydev debugger: process 8666 is connecting

/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py:125: RuntimeWarning: 'flask.cli' found in sys.modules after import of package 'flask', but prior to execution of 'flask.cli'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
 * Debugger is active!
 * Debugger PIN: 176-331-399
<...>
Creating Elasticsearch connection to [localhost:9200]
secrets path now: <...>
Using fake credentials from <...>
WARNING:root:Using dummy credentials manager as DEV_MODE is set to `true`
Starting metrics client on `('127.0.0.1', 8125)`
<...>
Creating Elasticsearch connection to [localhost:9200]
secrets path now: <...>
Using fake credentials from <...>
WARNING:root:Using dummy credentials manager as DEV_MODE is set to `true`
Starting metrics client on `('127.0.0.1', 8125)`
Using mock <...>
Using mock <...>

It's as if the application starts twice! I'm not sure how to debug this, how to analyse how the application starts up. Any advice?

ashald commented 4 years ago

Hi! Just from a glance at your logs, it seems that in the 1st example you have the Flask app running with debug off and in the 2nd one with debug on. Running the app with debug mode enables the reloader (as seen in https://stackoverflow.com/a/53790400) and that's why we can see:

* Restarting with stat

in 2nd example, as well as other logs that create an impression that app is started twice.

From my perspective, the difference that you're observing is solely the debug mode. What's unclear though is why it's enabled only in the latter run and not in former one. Could it be that you changed the setting in code or enabled it via an environment variable when you ran it with env file?

I'd be happy to help you to get to the bottom of this. This doesn't appear to be an issue with the plugin though so I'd close the issue. Please reopen if you think otherwise.

kmejka commented 2 years ago

Hey @ashald I wanted to return to this item, but I can't reproduce it :). Perhaps setting up the project locally another couple of items fixed it. Anyway - thanks for the help :)