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

Created on 12 Nov 2014  ยท  14Comments  ยท  Source: pallets/flask

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

โ”œโ”€โ”€ 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

Most helpful comment

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.

All 14 comments

Please check for stray test.pyc files. I used to be able to reproduce this before deleting the offending file in my pythonpath.

Unfortunatelly, running find . -name "*.pyc" -exec rm -rf {} \; before python -m test.api.hello --reloader does not make a difference

Is test visible in your PYTHONPATH?

PYTHONPATH=`pwd` python -m test.api.hello --reloader

@pgk: With your command it works.

I searched my environment for test.pyc files and I found the following (I'm using a virtualenv):

/home/kostas/Tmp/testenv/lib/python2.7/site-packages/setuptools/command/test.pyc
/home/kostas/Tmp/testenv/lib/python2.7/site-packages/werkzeug/testsuite/test.pyc
/home/kostas/Tmp/testenv/lib/python2.7/site-packages/werkzeug/test.pyc
/usr/local/lib/python2.7/dist-packages/py-1.4.23-py2.7.egg/py/test.pyc
/usr/lib/python2.7/dist-packages/setuptools/command/test.pyc
/usr/lib/python2.7/dist-packages/reportlab/graphics/barcode/test.pyc
/usr/lib/python2.7/dist-packages/chardet/test.pyc

Is there something wrong with my configuration? Or there is a conflict between some libraries?

Could you print sys.path in hello.py without the suggested command?

Here you are:

$ python -m test.api.hello
Starting with reloader=False
Sys path: [
    '',
    '/home/kostas/Tmp/testenv/lib/python2.7',
    '/home/kostas/Tmp/testenv/lib/python2.7/plat-x86_64-linux-gnu',
    '/home/kostas/Tmp/testenv/lib/python2.7/lib-tk',
    '/home/kostas/Tmp/testenv/lib/python2.7/lib-old',
    '/home/kostas/Tmp/testenv/lib/python2.7/lib-dynload',
    '/usr/lib/python2.7',
    '/usr/lib/python2.7/plat-x86_64-linux-gnu',
    '/usr/lib/python2.7/lib-tk',
    '/home/kostas/Tmp/testenv/local/lib/python2.7/site-packages',
    '/home/kostas/Tmp/testenv/lib/python2.7/site-packages'
]
 * Running on http://0.0.0.0:8080/

_Edit_: When running it with the suggested command, the first (empty) entry is replaced by the actual working directory.

I suppose that when running with --reloader, you hit https://github.com/mitsuhiko/werkzeug/issues/461 (i.e. the current working directory disappears from the path, and the script's directory is added)

Just wanted to note that I'm having the exact same issue, I switched my project structure around so I wouldn't have to add to PYTHONPATH, but it looks like I'm going to have to if I want to use reloader ?

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

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" [email protected] 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

@untitaker better solution is to run the app with

PYTHONPATH=`pwd` python -m test.api.hello --reloader

just as @pgk suggested. Actually, this is the only way that works for me.

That actually works, Thanks!

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.

We now encourage you to install your project in editable mode during development. pip install -e . This, and using the flask command, should fix any import issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chuanconggao picture chuanconggao  ยท  4Comments

maangulo12 picture maangulo12  ยท  4Comments

davidism picture davidism  ยท  3Comments

sungjinp11 picture sungjinp11  ยท  3Comments

ghostbod99 picture ghostbod99  ยท  4Comments