安装了flask_sqlalchemy模块,但仍然出现相同的错误ModuleNotFoundError:没有名为“ flask_sqlalchemy”的模块

时间:2020-01-30 10:18:13

标签: python-3.x postgresql flask orm flask-sqlalchemy

我正在研究 CS50 Beyond ...在线课程,可以在 YouTube上通过Yu.Brian Yu教授 该代码来自 models.py 文件中的Lecture ORM和API。

我不明白此错误

(venv) (base) pglab@pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ export FLASK_APP=application.py 
(venv) (base) pglab@pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3.7 application.py 
(venv) (base) pglab@pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ flask run
* Serving Flask app "application.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: While importing "application", an ImportError was raised:

Traceback (most recent call last):
  File "/home/pglab/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/pglab/Downloads/Compressed/orms_and_apis/airline2/application.py", line 2, in <module>
    from models import *
  File "/home/pglab/Downloads/Compressed/orms_and_apis/airline2/models.py", line 4, in <module>
    from flask_sqlalchemy import SQLAlchemy
ModuleNotFoundError: No module named 'flask_sqlalchemy'

我也检查了所有设置,

(venv) (base) pglab@pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0

(venv) (base) pglab@pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ pip install Flask
Requirement already satisfied: Flask in /home/pglab/venv/lib/python3.7/site-packages (1.1.1)

(venv) (base) pglab@pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3 -m pip install sqlalchemy
Requirement already satisfied: sqlalchemy in /home/pglab/venv/lib/python3.7/site-packages (1.3.13)

一切似乎都很好!我运行了烧瓶应用程序很多次,但是当我在烧瓶中启动ORM程序时,事情变得混乱了。

有人可以帮我吗!

2 个答案:

答案 0 :(得分:0)

我想您已经在虚拟环境之外安装了SQLalchemy。 请在激活虚拟环境后安装它,然后只有虚拟环境才能搜索模块。 主要原因是虚拟环境的范围受到限制,而不是全局的

请遵循以下命令

source/bin/activate   //this will virtual enviroment
sudo pip install Flask-SQLAlchemy==2.4.1 // this will install the packge within your virtual envoronment

答案 1 :(得分:0)

要获取用于SQLAlchemy的Flask包装器,请

python3 -m pip install flask-sqlalchemy

代替

python3 -m pip install sqlalchemy

或者,由于您已经安装了SQLAlchemy,因此只需执行前者即可。