Python 导入错误 - 没有模块

Python 导入错误 - 没有模块

抱歉,这是基础问题。我按照一个相当简单的教程操作,在本地运行 flask 时出现此错误:

Traceback (most recent call last):
  File "/Users/james/opt/anaconda3/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/Users/james/Documents/GitHub/flask-app-blueprint/project/__init__.py", line 3, in <module>
    from flask_sqlalchemy import SQLAlchemy
ModuleNotFoundError: No module named 'flask_sqlalchemy'

然而,在相同的环境中,当我运行 pip list 时,它告诉我有该模块:Flask-SQLAlchemy 2.2

(venv) (base) james flask-app-blueprint % pip3 install flask-sqlalchemy
Requirement already satisfied: flask-sqlalchemy in ./venv/lib/python3.8/site-packages (2.5.1)
Requirement already satisfied: Flask>=0.10 in ./venv/lib/python3.8/site-packages (from flask-sqlalchemy) (1.0.2)
Requirement already satisfied: SQLAlchemy>=0.8.0 in ./venv/lib/python3.8/site-packages (from flask-sqlalchemy) (1.3.3)
Requirement already satisfied: Jinja2>=2.10 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (2.11.3)
Requirement already satisfied: itsdangerous>=0.24 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (0.24)
Requirement already satisfied: Werkzeug>=0.14 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (2.0.1)
Requirement already satisfied: click>=5.1 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (7.0)
Requirement already satisfied: MarkupSafe>=0.23 in ./venv/lib/python3.8/site-packages (from Jinja2>=2.10->Flask>=0.10->flask-sqlalchemy) (2.0.1)

最重要的是,根据我所看到的内容,我文件中的代码看起来是正确的:

# IMPORTS
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

我可能哪里做错了?

答案1

问题似乎是,当你运行你的 flask 脚本时,它使用的环境(anaconda)没有flask_sqlalchemy安装该包,这导致脚本失败,但是当你检查它是否安装时,你是在venv确实安装了它的虚拟环境中执行此操作的。

相关内容