运行flask应用提示找不到模块

wffger
  • 203

环境变量已设置,初始化文件已创建,为什么还是提示找不到模块?

(Flask-Practice) ydx@ydx-PC:$ ls
app  config  config.py  instance  LICENSE.md  README.md  requirements.txt
(Flask-Practice) ydx@ydx-PC:$ ls app
auth.py  database.py  forms.py  __init__.py  login.py  __pycache__  scheme.sql  static  templates  views
(Flask-Practice) ydx@ydx-PC:$ echo $FLASK_APP
app
(Flask-Practice) ydx@ydx-PC:$ flask run
Traceback (most recent call last):
  File "/opt/python-env/Flask-Practice/bin/flask", line 11, in <module>
    sys.exit(main())
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/flask/cli.py", line 478, in main
    cli.main(args=args, prog_name=name)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/flask/cli.py", line 345, in main
    return AppGroup.main(self, *args, **kwargs)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/click/core.py", line 1060, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/flask/cli.py", line 388, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/flask/cli.py", line 124, in __init__
    self._load_unlocked()
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/flask/cli.py", line 148, in _load_unlocked
    self._app = rv = self.loader()
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/flask/cli.py", line 209, in load_app
    rv = locate_app(self.app_import_path)
  File "/opt/python-env/Flask-Practice/lib/python3.6/site-packages/flask/cli.py", line 89, in locate_app
    __import__(module)
ModuleNotFoundError: No module named 'app'

退出虚拟环境可以正常创建Flask应用:

(Flask-Practice) ydx@ydx-PC:$ deactivate
ydx@ydx-PC:$ ls
app  config  config.py  instance  LICENSE.md  README.md  requirements.txt
ydx@ydx-PC:$ flask run
 * Serving Flask app "app" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 146-573-781
127.0.0.1 - - [11/Jul/2018 00:34:08] "GET /hello HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/opt/anaconda3/lib/python3.6/site-packages/flask/cli.py", line 76, in find_best_app
    app = call_factory(script_info, app_factory)
  File "/opt/anaconda3/lib/python3.6/site-packages/flask/cli.py", line 116, in call_factory
    return app_factory()
  File "/home/ydx/Documents/Project/Python-Flask-Practice/app/__init__.py", line 27, in create_app
    from . import database
  File "/home/ydx/Documents/Project/Python-Flask-Practice/app/database.py", line 2, in <module>
    from flask_sqlalchemy import SQLAlchemy
ModuleNotFoundError: No module named 'flask_sqlalchemy'
127.0.0.1 - - [11/Jul/2018 00:34:08] "GET /hello?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2018 00:34:08] "GET /hello?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2018 00:34:08] "GET /hello?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2018 00:34:08] "GET /hello?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2018 00:34:08] "GET /hello?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2018 00:34:08] "GET /hello?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
回复
阅读 4.3k
2 个回答
✓ 已被采纳

重装flask,解决问题。

clipboard.png

你定义的是run.py,把 hello 改成run试试。


刚看到题主用的应该是Linux。

请使用 echo $FLASK_APP 检查环境变量是否正确设置。

宣传栏