【发布时间】:2019-01-08 05:58:37
【问题描述】:
尝试运行具有以下导入的文件:
from flask_sqlalchemy import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
收到以下错误:
ImportError: No module named 'flask_sqlalchemy'
SQLAlchemy 已安装。尽管如此,我还是尝试重新安装到将要使用它的目录中。我得到了这个:
The directory '/Users/_/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/_/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: Flask-SQLAlchemy in /Library/Python/2.7/site-packages (2.3.2)
Requirement already satisfied: Flask>=0.10 in /Library/Python/2.7/site-packages (from Flask-SQLAlchemy) (1.0.2)
Requirement already satisfied: SQLAlchemy>=0.8.0 in /Library/Python/2.7/site-packages (from Flask-SQLAlchemy) (1.2.10)
Requirement already satisfied: Jinja2>=2.10 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (2.10)
Requirement already satisfied: itsdangerous>=0.24 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (0.24)
Requirement already satisfied: Werkzeug>=0.14 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (0.14.1)
Requirement already satisfied: click>=5.1 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (6.7)
Requirement already satisfied: MarkupSafe>=0.23 in /Library/Python/2.7/site-packages (from Jinja2>=2.10->Flask>=0.10->Flask-SQLAlchemy) (1.0)
关于我不拥有该目录的说法是不正确的。我是这台机器上唯一的人。我拥有一切。
无论如何,我回去重新运行文件并得到相同的错误消息。因此,它已安装,但未安装,或者至少对我不可用。
当我注释掉其中一个导入语句时看到的一条错误消息如下:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py
我不知道如何解决这个问题并让 SQLAlchemy 启动并运行。我已经烧了超过1.5个小时。列出的最后一个错误表明有 2 个版本的 python 可能与它有关。
您对补救措施的想法将不胜感激。
【问题讨论】:
-
也有可能你的 pip 和 python 版本不匹配(即你仍然指向 python2 的 pip,所以它认为 flask_sqlalchemy 已安装,但随后使用 python3 运行)。如果不查看您的环境,很难判断这里发生了什么
-
您能否显示
which python的输出(或显示在您的cmd 行上执行python时出现的REPL)。并给出pip --version的输出? -
是的,我怀疑你正在为 python2.7 运行带有 pip 的 python3。所以没有为 Python3 正确安装这些库。您可以使用 python2 运行,也可以使用
pip3并重新安装您的库 -
如果您无法更改正在使用的 pip,那么您需要确保您使用的是 Python2,而不是 Python3。您已经安装了 Python2 的库(据我所知)。
-
等等,不是 PYTHONPATH,而是获取 Python2 而不是 Python3 的常规 PATH...PYTHONPATH 只能用于指向 Python 库(在运行 python 之后),但它不会告诉你的操作系统启动哪个 Python
标签: python sqlalchemy flask-sqlalchemy