question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flask fails to start with use_reloader=True when using absolute import

See original GitHub issue

I’m using Flask v0.10.1 and I have the following project structure:

test
├── api
│   ├── hello.py
│   └── __init__.py
├── config
│   ├── config.py
│   └── __init__.py
└── __init__.py

The hello.py file is:

import sys
import flask
from test.config import config

app = flask.Flask(__name__)

@app.route('/hello')
def hello():
    return config.MESSAGE

def main(argv):
    reloader = '--reloader' in argv
    print('Starting with reloader={}'.format(reloader))
    app.run(host='0.0.0.0', port=8080, debug=True, use_reloader=reloader)

if __name__ == '__main__':
    main(sys.argv)

and the config.py is simply

MESSAGE = 'Hello!'

When I run api.py with use_reloader=False (python -m test.api.hello) the server starts correctly. If I run it with use_reloader=True (python -m test.api.hello --reloader) it fails with:

Starting with reloader=True
 * Running on http://0.0.0.0:8080/
 * Restarting with reloader
Traceback (most recent call last):
  File "/home/kostas/Tmp/testproj/test/api/hello.py", line 3, in <module>
    from test.config import config
ImportError: No module named config

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
bootccommented, Jun 26, 2015

I personally fixed this using the following in my equivalent of hello.py:

# Workaround for the werkzeug reloader removing the current directory from the
# path. It's nasty, but it works! Inspired by:
# https://github.com/mitsuhiko/flask/issues/1246
os.environ['PYTHONPATH'] = os.getcwd()

Just do that before you call app.run() with the reloader enabled; that seems to fix it for me.

2reactions
untitakercommented, Jun 18, 2015

The current solution is to avoid running your app via python -m ....

The issue I linked above contains all the details and why this is an unfixable problem AFAIK

On 18 June 2015 11:59:41 CEST, “Piotrek Szymański” notifications@github.com wrote:

Same issue here, app fails to load absolute import when using debug=True. Debug=False works fine.


Reply to this email directly or view it on GitHub: https://github.com/mitsuhiko/flask/issues/1246#issuecomment-113097027

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to import flask app folder in unittest folder of an repo
I have tried the following after checking a few resources of absolute and relative imports, none of them worked. from .. import app....
Read more >
Flask absolute import bug in debug mode - Chase Seibert Blog
I was getting errors trying to use absolute imports in a new flask app: ... It turned out that setting debug=False fixed the...
Read more >
How we improved our Python backend start-up time - Medium
Two months ago, it was taking around 10 seconds for our backend to start the application on a developer's machine, and around 12...
Read more >
API — Flask Documentation (2.2.x)
Relative to the application root_path or an absolute path. ... When using flask run to start the development server, an interactive debugger will...
Read more >
Flask-Testing 0.3 documentation - PythonHosted.org
from flask import Flask from flask_testing import TestCase class ... be set to 0 to have the underlying operating system pick an open...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found