Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mavroprovato opened this issue Nov 12, 2014 · 14 comments
Closed

Comments

@mavroprovato
Copy link

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
@untitaker
Copy link
Contributor

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

@mavroprovato
Copy link
Author

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

@pgk
Copy link

pgk commented Nov 13, 2014

Is test visible in your PYTHONPATH?

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

@mavroprovato
Copy link
Author

@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?

@untitaker
Copy link
Contributor

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

@mavroprovato
Copy link
Author

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.

@untitaker
Copy link
Contributor

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

@coilysiren
Copy link

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 ?

@b1-88er
Copy link

b1-88er commented Jun 18, 2015

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

@untitaker
Copy link
Contributor

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:
#1246 (comment)

@mavroprovato
Copy link
Author

@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.

@b1-88er
Copy link

b1-88er commented Jun 19, 2015

That actually works, Thanks!

@bootc
Copy link

bootc commented 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.

FND added a commit to FND/taskard that referenced this issue Oct 4, 2015
note, however, that due to an issue with Werkzeug's reloader, debug mode
still requires a separate script:
pallets/flask#1246
@davidism
Copy link
Member

davidism commented May 28, 2017

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants