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

Relative import doesn't work in debug mode #1523

Closed
benjamin-hodgson opened this issue Jul 1, 2015 · 5 comments
Closed

Relative import doesn't work in debug mode #1523

benjamin-hodgson opened this issue Jul 1, 2015 · 5 comments

Comments

@benjamin-hodgson
Copy link

I get an exception when I attempt to use a relative import with Flask in debug mode.

Steps to reproduce

My package looks like this:

mypkg\
  __init__.py
  mymodule.py

Here's __init__.py:

xyz = 123

And in mymodule.py:

from flask import Flask
from . import xyz

app = Flask(__name__)
app.run(debug=True)

Run the application using python -m mypkg.mymodule. I'm using Python 3.4.3 on a Windows machine, with Flask 0.10.1 (the version from PyPI).

Actual results

The usual "you attempted to use a relative import but you're not in a package" exception:

Traceback (most recent call last):
  File "...\mymodule.py", line 2, in <module>
    from . import xyz
SystemError: Parent module '' not loaded, cannot perform relative import

If you remove debug=True, or use an absolute import (from mypkg import xyz), there is no error. The error persists if you name your module __main__.py (instead of mymodule.py) and use python -m mypkg. The error persists if you attempt to import a name from a submodule of the package (it's nothing to do with the import being from __init__.py).

I'm guessing it's something to do with Flask's magical reloading of edited files in debug mode.

Expected results

No error; xyz is imported as usual.

@ThiefMaster
Copy link
Member

Are you sure that this is a Flask issue? Check if http://stackoverflow.com/questions/16981921/relative-imports-in-python-3 helps you.

@untitaker
Copy link
Contributor

No, this is an unfixable bug in Werkzeug. See pallets/werkzeug#461

@benjamin-hodgson
Copy link
Author

😞

@davidism
Copy link
Member

davidism commented Jul 1, 2015

@benjamin-hodgson while the bug is "unfixable", there is a solution: don't try to run a Flask app with python -m.

@umed

This comment has been minimized.

@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

5 participants