I'm trying to run the "flaskr" and "minitwit" examples, but I'm running into an import error issue. I'm trying to follow the instructions from the README file. When running flask initdb, I get the following error:
Traceback (most recent call last):
File "/home/nelson/apps/miniconda/bin/flask", line 6, in <module>
sys.exit(flask.cli.main())
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/flask/cli.py", line 478, in main
cli.main(args=args, prog_name=name)
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/flask/cli.py", line 345, in main
return AppGroup.main(self, *args, **kwargs)
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/click/core.py", line 1055, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args)
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/click/core.py", line 1094, in resolve_command
cmd = self.get_command(ctx, cmd_name)
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/flask/cli.py", line 316, in get_command
rv = info.load_app().cli.get_command(ctx, name)
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/flask/cli.py", line 209, in load_app
rv = locate_app(self.app_import_path)
File "/home/nelson/apps/miniconda/lib/python3.4/site-packages/flask/cli.py", line 89, in locate_app
__import__(module)
ImportError: No module named 'flaskr'
I'm using an Anaconda Python install on both Windows 10 (Python 3.5) and Linux (Python 3.4) with either the stock flask 0.11 install or an install from the latest git repo. I'm running this code from appropriate directory and am able to import flaskr or minitwit from a terminal Python session. I can run the other examples that do not require the flask script.
The flaskr example should be modified to include a proper setup.py.
Okay. I'm trying to figure this out, but I hit a snag. If I get this working, I could file a PR.
I moved flaskr.py into a new folder "flaskr" and created an empty __init__.py file. I then created the following setup.py file:
from setuptools import setup, find_packages
setup(
name='flaskr',
description='Simple microblog example using Flask',
packages=find_packages(),
entry_points='''
[flask.commands]
initdb=flaskr.flaskr:initdb_command
''',
)
In a new conda environment, I installed the development version of flask and did a pip install of the flaskr folder containing the setup.py. Now when I run flask initdb, I get the following error.
Usage: flask initdb [OPTIONS]
Error: Failed to find application in module "flaskr". Are you sure it contains a Flask application? Maybe you wrapped it in a WSGI middleware or you are using a factory function.
Any hints on where to go next? I'm looking at the docs, and it seems like this should work now.
The extension in the FLASK_APP env variable is removed in the documentation.
export FLASK_APP=flaskr
should be
export FLASK_APP=flaskr.py
That seems to have fixed it for me.
@badawik Yes that's also a possibility but doesn't reflect the best practices that the docs should present. It is a good upgrade path for older apps though.
I had successfully run the "flaskr" and "minitwit" examples on Windows 10 (Python 3.5) with commands:
$ set FLASK_APP=flaskr
$ python -m flask initdb
$ python -m flask run
The solution by @badawik also worked for me. I cloned the master repo and ran flask initdb and recieved the same error as @rnelsonchem. The tutorial says that you should export FLASK_APP=flaskr so I think this may need to be updated.
@untitaker If you get a chance, please take a look at #1925. I refactored _flaskr_ into a package with a _setup.py_ and I'm looking for some feedback. I am still working updating the tutorial in the docs to reflect the changes ( which I will add in another commit to #1925 ).
At the present time there is no way for anybody to complete the Flask tutorial - its broken because of this issue. Inside or outside of a virtualenv, on Python 2.7 or 3.5, the "flask run" command will not be able to find the flaskr.py and thus won't work. These steps:
export FLASK_APP=flaskr
export FLASK_DEBUG=1
flask run
Will never work because there is no way for Python to find the flaskr.py file. The fix we are using is:
export PYTHONPATH=`pwd`
Or similar, and then of course the tutorial works. In the past the tutorial used to involve using python to run the flaskr.py file itself, not sure why this changed to the 'flask' binary but the change means people can't complete the tutorial...thereby damaging Flask's reputation!
export FLASK_APP=flaskr.py
export FLASK_DEBUG=1
flask run
Then flask run in the _flaskr_ directory works just fine. But yes, the docs and example do need some updating. (See PR in my comment above.)
@davidism can this be closed? #1945 #1954
@wgwz Are you sure? #1954 is still open
I see that the Flaskr tutorial for the development version of Flask has been updated, but that the equivalent 0.11 tutorial has not. Does this mean that the Flask 0.11 Flaskr tutorial will be permanently unusable?
If so, perhaps it would be good to update the Flask website so that someone clicking through the homepage->docs->tutorial does not end up on the 0.11 tutorial version?
Docs are only updated at release time, in principle. We should do a bugfix release soon.
On 31 July 2016 12:33:23 CEST, tigertailzlc [email protected] wrote:
I see that the Flaskr tutorial for the development version of Flask has
been updated, but that the equivalent 0.11 tutorial has not. Does this
mean that the Flask 0.11 Flaskr tutorial will be permanently unusable?If so, perhaps it would be good to update the Flask website so that
someone clicking through the homepage->docs->tutorial does not end up
on the 0.11 tutorial version?
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
https://github.com/pallets/flask/issues/1902#issuecomment-236422669
Sent from my Android device with K-9 Mail. Please excuse my brevity.
This is fixed in master I think, via #1945 and #1954. Thanks @wgwz for the excellent work!
I am going through tutorial and it does not work for me (Flask 0.11.1, Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)]). Thanks for all suggestions, I've used this and it works:
# Project directory
$ export FLASK_APP=flaskr/flaskr.py; export FLASK_DEBUG=1; flask.exe run
Hi! I'm also following the tutorial and I'm not able to execute the flask dbinit command.
I've got Flask 0.11.1 and python 2.7, and I'm obtaining the same error commented by @rnelsonchem
Any hint? I think I could workaround it but would like to follow the tutorial step by step.
Thanks in advance!
Please check if the docs from master fix that.
I see that this issue is closed but the 0.11 tutorial walkthrough is still broken leading to ImportError: No module named 'flaskr' and probably people giving up on it. Only someone who tries searching for the error will even come across this github issue and be directed to click on the development docs.
The docs from master do fix the import error. At least as far as I can tell. If you are running into this issue please follow here:
http://flask.pocoo.org/docs/dev/tutorial/packaging/
I will create a PR for this to get into 0.11. I thought I had done that, but I guess not.
I solved this issue running the command at the same folder where ___init__.py_ and _flaskr.py_ are placed.
Had to search for this issue to solve my problem today.
@untitaker let's figure this out. see my previous comment in this thread. should i submit something to the 0.11 branch?
Would be nice, though I think I'm just going to release 0.12 tomorrow.
Ok 0.12 is out, hopefully I'll never get emails from this thread again :P
@rnelsonchem I have exactly the same issue with this tutorial. How did you solve it ?
Hey there. Just followed the tutorial, but to save time and copy-pasting i took the finished flaskr app from the examples folder. It does not work with the installation commands provided in the tutorial.
EDIT:
I just re-downloaded the master, reinstalled the example and still:
flask.cli.NoAppException: Failed to find application in module "flaskr". Are you sure it contains a Flask application? Maybe you wrapped it in a WSGI middleware or you are using a factory function.
I am running the commands from the root flaskr folder. The commands were:
export FLASK_APP=flaskr
export FLASK_DEBUG=true
flask run
also, the flask initdb command does not work:
Usage: flask [OPTIONS] COMMAND [ARGS]...
Error: No such command "initdb".
EDIT 2:
Got it now. One needs to checkout the correct version of the repo. For others having similar problems see #2437. Thanks for not bashing the newbie 馃槂
I had a hard time with this. I hate to use this as a support forum, but maybe this helps someone else (as of April 2018) if they are googling for help.
I'm on Fedora 27 using pkg version of python3 (3.6.5 as of writing) + flask
Follow all the tutorial instructions up to the point where you set ENV variables - then do this:
``
export FLASK_APP=flaskr
export FLASK_DEBUG=1 #if you want debug out
export FLASK_ENV=development
python3 -m flask initdb
python3 -m flask run
``
Oh and login is "admin","default"
I'm on a Mac and doing the tutorial in a miniconda environment. I think the miniconda environment broke things for me.
Not a solution I've seen here so I'm sharing. Hardcoding the FLASK_APP path did the trick for me:
export FLASK_APP=~/miniconda/envs/myenv/myenv/flask-tutorial/flaskr
Environment variables are always a hassle. :-/
I can run the Flask app on tag 1.1.2 but the tests won't run, even if I change the end to flaskr.py as suggested above.
Wierdly Flaskr is installed:
(venv) $ pip install '.[test]'
...
Installing collected packages: flaskr
Found existing installation: flaskr 1.0.0
Uninstalling flaskr-1.0.0:
Successfully uninstalled flaskr-1.0.0
Running setup.py install for flaskr ... done
Successfully installed flaskr-1.0.0
WARNING: You are using pip version 19.2.3, however version 20.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) $ pytest
ImportError while loading conftest '/home/me/source/flask/examples/tutorial/tests/conftest.py'.
tests/conftest.py:6: in <module>
from flaskr import create_app
E ModuleNotFoundError: No module named 'flaskr'
(venv) $ pip list
Package Version Location
------------------ ------- ---------------------------
...
Flask 1.1.2 /home/greg/source/flask/src
flaskr 1.0.0
...
It runs if I do python -m pytest. Is the problem venv?
The problem appears to be that your pytest is cached to be something outside your venv, probably because you tried to run pytest, then activated the venv, then tried to run it again. hash -r will fix that. The fact that python -m pytest works just means that you didn't mistakenly try to run python itself outside the venv, otherwise you'd run into the same issue. That's an issue with how Bash caching works, not with the tutorial.
I am absolutely sure that if you follow the tutorial as written currently, it works on Bash, so I don't plan to change this.
Most helpful comment
The extension in the FLASK_APP env variable is removed in the documentation.
export FLASK_APP=flaskrshould be
export FLASK_APP=flaskr.pyThat seems to have fixed it for me.