简体   繁体   English

为什么我需要在 Heroku 中进行相对导入,但在本地运行时不需要?

[英]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.

相关问题 使用heroku的“ ImportError:无法导入Django”,但不在本地运行时 - “ImportError: Couldn't import Django” using heroku, but not while running locally 在本地使用 Dask 时需要初始化客户端吗? - Do I need to initialize the client when using Dask locally? 以 package 或非运行时如何相对导入? - How to relative import when running as package or when not? 为什么在我导入模块时 Python 正在运行我的模块,我该如何停止它? - Why is Python running my module when I import it, and how do I stop it? 为什么我必须在包的 __init__.py 中使用相对导入? - Why do I have to use a relative import in a package's __init__.py? 在本地运行“ python manage.py syncdb”时出错,但通过Heroku运行相同的命令时没有错误 - Error when running “python manage.py syncdb” locally, but no error when running the same command via Heroku 为什么我的django开发服务器在本地运行的速度要比在Heroku上运行同一开发服务器的速度快得多? - Why is my django development server running locally so much faster then running the same development server on Heroku? 在运行Python代码时尝试以非包形式进行相对导入 - Attempted relative import in non-package when running a Python code 在运行 CrawlerProcess 时尝试在没有已知父 package 的情况下进行相对导入 - attempted relative import with no known parent package when running CrawlerProcess 运行pip / easy_install时是否需要sudo? - Do I need to sudo when running pip/easy_install?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM