cmd执行flask shell显示 Usage: flask shell [OPTIONS] Error: Could not import "app".

想向greyli以及各位同学请教一个问题,我已经搜索过论坛和Google,但是无果
我的阅读进度在本书的5.3.3的第一个代码段
错误显示为

(flask-exercise-w9CIZiES) C:\Users\jnjga\flask-exercise\demos\database>flask shell
Usage: flask shell [OPTIONS]

Error: Could not import "app".

包依赖如下

(flask-exercise-w9CIZiES) C:\Users\jnjga\flask-exercise\demos\database>pipenv graph
Flask-SQLAlchemy==2.4.0
  - Flask [required: >=0.10, installed: 1.1.1]
    - click [required: >=5.1, installed: 7.0]
    - itsdangerous [required: >=0.24, installed: 1.1.0]
    - Jinja2 [required: >=2.10.1, installed: 2.10.1]
      - MarkupSafe [required: >=0.23, installed: 1.1.1]
    - Werkzeug [required: >=0.15, installed: 0.15.4]
  - SQLAlchemy [required: >=0.8.0, installed: 1.3.8]
Flask-WTF==0.14.2
  - Flask [required: Any, installed: 1.1.1]
    - click [required: >=5.1, installed: 7.0]
    - itsdangerous [required: >=0.24, installed: 1.1.0]
    - Jinja2 [required: >=2.10.1, installed: 2.10.1]
      - MarkupSafe [required: >=0.23, installed: 1.1.1]
    - Werkzeug [required: >=0.15, installed: 0.15.4]
  - WTForms [required: Any, installed: 2.2.1]
python-dotenv==0.10.3

该文件夹下的内容如下

(flask-exercise-w9CIZiES) C:\Users\jnjga\flask-exercise\demos\database>dir
 驱动器 C 中的卷是 Windows
 卷的序列号是 F62E-77FA

 C:\Users\jnjga\flask-exercise\demos\database 的目录

2019/08/30  21:00    <DIR>          .
2019/08/30  21:00    <DIR>          ..
2019/08/30  21:00                13 .flaskenv
2019/08/30  20:56               423 app.py
2019/08/30  19:39    <DIR>          templates
2019/08/30  20:05    <DIR>          __pycache__
               2 个文件            436 字节
               4 个目录 55,455,485,952 可用字节

app.py内容如下

from flask import Flask,url_for,request
from flask_sqlalchemy import SQLAlchemy
import os

app=Flask(__name__)

db=SQLAlchemy(app)

#配置URI

app.config['SQLALCHEMY_DATABASE_URI']=os.getenv('DATABASE_URL','sqlite:///'+os.path.join(app.root_path,'data.db'))
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False

class Note(db.Model):
    id=db.Column(db.Integer,primary_key=True)
    body=db.Column(db.Text)

这里是有app的为什么会显示无法导入app呢?

另外,请问如何进入pipenv安装的包依赖环境下进入python shell呢?(也即5.3.1中的第二个代码段)
十分感谢您的阅读,如果能帮忙解决此问题,不胜感激

更新

第二天我试图进入我自己仿照前几章所写的目录下,比如前两章我写的代码目录C:\Users\jnjga\flask-exercise\demos\basic(这部分代码之前使用各种命令是没有问题的),试图使用flask shell 仍显示 could not import “app”
如果我使用flask run 可以运行,但是访问localhost:5000时,会返回如下错误

Traceback (most recent call last):
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
ModuleNotFoundError: No module named 'app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\cli.py", line 338, in __call__
    self._flush_bg_loading_exception()
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\cli.py", line 326, in _flush_bg_loading_exception
    reraise(*exc_info)
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\cli.py", line 314, in _load_app
    self._load_unlocked()
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "c:\users\jnjga\.virtualenvs\flask-exercise-w9cizies\lib\site-packages\flask\cli.py", line 250, in locate_app
    raise NoAppException('Could not import "{name}".'.format(name=module_name))
flask.cli.NoAppException: Could not import "app".

这是一个与上方相似的错误
我怀疑这是由于最近安装在pipenv环境中的包造成的,如flask-sqlalchemy,但我无法定位原因

已经解决
结局方法:
pipenv --rm删除虚拟环境
手动删除pipfile、pipfle.lock
重新pipenv install
重新安装各依赖包

1 个赞