[英]Why do I need a relative import in Heroku but not when running locally?
When running locally my Flask app, I can use a common import:在本地运行我的 Flask 应用程序时,我可以使用通用导入:
from models import MyModel
But when running on Heroku, I need to change it to:但是在 Heroku 上运行时,我需要将其更改为:
from .models import MyModel
Why does that happen?为什么会这样?
I'm not sure why on Heroku this happens.我不确定为什么在 Heroku 上会发生这种情况。 But it has to do with the Python path.
但它与 Python 路径有关。
If you specify the path where the app is on the Procfile, it will work.如果您指定应用程序在 Procfile 上的路径,它将起作用。
Before:前:
web: gunicorn src.api:api
After:后:
web: gunicorn src.api:api --pythonpath=./src
Considering that the main file is named api.py
and is located inside a folder called src
.考虑到主文件名为
api.py
并且位于名为src
的文件夹内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.