gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:00 34 4
gpt4 key购买 nike

在顺利地将我的 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com