python - 导入错误 : cannot import name 'SQLAlchemy' from 'flask_sqlalchemy' (unknown location)

标签 python heroku sqlalchemy flask-sqlalchemy

在顺利地将我的 python 应用程序推送到 heroku 之后,我发出了 heroku ps:scale web=1 命令并在 heroku 日志中遇到以下错误;

2018-11-27T08:34:03.963364+00:00 app[web.1]: from flask_sqlalchemy import SQLAlchemy
2018-11-27T08:34:03.971768+00:00 app[web.1]: ImportError: cannot import name 'SQLAlchemy' from 'flask_sqlalchemy' (unknown location)
2018-11-27T08:34:03.972159+00:00 app[web.1]: [2018-11-27 08:34:03 +0000] [11] [INFO] Worker exiting (pid: 11)
2018-11-27T08:34:04.163955+00:00 app[web.1]: Traceback (most recent call last):

尽管使用以下步骤/命令为 sqlalchemy 设置了虚拟环境;

virtualenv sqlalchemy
source flask.ext.sqlalchemy/bin/activate
pip install sqlalchemy
deactivate

virtualenv flask_sqlalchemy
source flask_sqlalchemy/bin/activate
pip install flask-sqlalchemy
deactivate

在我的 python 代码中使用以下导入;

import flask_sqlalchemy
from flask_sqlalchemy import SQLAlchemy

和所需的 Heroku 文件;

Procfile 的内容:

web: gunicorn project:app  --log-file - --log-level debug

requirements.txt的内容;

Flask
gunicorn

runtime.txt的内容;

python-3.7.1

找不到 SQLAlchemy。

请找到我的代码 here

python 应用程序在本地机器上运行良好,但在 Heroku 上运行不佳。是否有适当的方法来安装 SQLAlchemy 以正确导入?或者还有其他方法吗?

最佳答案

来自 heroku documentation :

The requirements.txt file lists the app dependencies together. When an app is deployed, Heroku reads this file and installs the appropriate Python dependencies using the pip install -r command.

您的 github 存储库有一个 requirements.txt,但它缺少依赖项,例如 SQLAlchemy 和您可能在本地安装的其他一些依赖项。

要保存您在本地安装的所有包,请打开命令行并转到项目目录(如果您使用的是 virtualenv,首先激活该环境,然后使用命令行转到项目目录)并键入:

pip freeze > requirements.txt

这将保存您在本地安装的所有包。

关于python - 导入错误 : cannot import name 'SQLAlchemy' from 'flask_sqlalchemy' (unknown location),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495739/

相关文章:

sqlalchemy - 在 SQL Alchemy (1.0.4) 中获取表列

python - SQLAlchemy,以惯用的 Python 方式隔离和重试可序列化的事务

python - 无法解码转义字符串python

ruby-on-rails - 如何停止 heroku db 进程

python - Apache Beam GroupByKey() 在 Python 中的 Google DataFlow 上运行时失败

node.js - 如何在 Heroku 上将 http2 与 Node 一起使用(使用 Koa)

javascript - Heroku 应用程序在缺少 Node 模块时出错(但看起来它已安装)

SQLAlchemy 引发完整性错误, "update or delete on table violates foreign key constraint"

python - 如何在获得输出后立即停止 python.exe 关闭?

python - 使用 Python 中的请求将文件上传到 Google 云端硬盘