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

flaskr project (in dev docs and on github) needs to use relative import in __init__.py for Python 3 #2051

Closed
toddjames opened this issue Oct 8, 2016 · 8 comments

Comments

@toddjames
Copy link

Currently, while following the dev docs or cloning the existing flaskr project on GitHub, it will not run in a Python 3 virtualenv. The error when running the cloned flaskr application is:

Traceback (most recent call last): File "/home/todd/flaskr/flask/examples/flaskr/env/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise raise value File "/home/todd/flaskr/flask/examples/flaskr/flaskr/__init__.py", line 1, in <module> from flaskr import app ImportError: cannot import name 'app'

This is the same error when following the docs to recreate the flaskr application. By modifying flaskr/__init__.py to include a relative import when using a Python 3 virtualenv, everything works fine.

That is, in flaskr/__init__.py, change:
from flaskr import app
to:
from .flaskr import app

(note the period)

At this point, I am still not comfortable contributing, nor do I know if this is the correct solution or if this has any implications for Python 2. Hence the issue, hoping someone else can confirm this is correct, or provide a better solution.

@wgwz
Copy link
Contributor

wgwz commented Oct 8, 2016

Good catch, this is my mistake. That line should be from flaskr.flaskr import app. Does this import statement fix it for you?

@toddjames
Copy link
Author

Yes, that also fixes it.

Heasummn added a commit to Heasummn/flask that referenced this issue Oct 8, 2016
Heasummn added a commit to Heasummn/flask that referenced this issue Oct 8, 2016
@toddjames
Copy link
Author

Thanks for the quick fix to the project itself. You may want to fix the documentation on this page, too:

http://flask.pocoo.org/docs/dev/tutorial/packaging/#tutorial-packaging

@untitaker
Copy link
Contributor

That should eventually update automatically.

@wgwz
Copy link
Contributor

wgwz commented Oct 9, 2016

@untitaker Are there tools available to link parts of a code example to documentation? That would be quite nice.

@untitaker
Copy link
Contributor

What do you mean?

@untitaker
Copy link
Contributor

Are you talking about the examples directory in the repo, or inline code examples?

@georgeberry
Copy link

georgeberry commented Dec 24, 2016

I found this error as well. This is my (working) __init__.py file:

from flask import Flask
app = Flask(__name__)

from . import views # relative import to fix github issue #2058

@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

4 participants