Getting error "ImportError: No module named" on Heroku but not locally

Getting error "ImportError: No module named" on Heroku but not locally

当我尝试将我的应用程序部署到 heroku 但该应用程序在本地构建良好时,我得到 ImportError: No module named。这是来自 Heroku

的日志
Traceback (most recent call last):
File "mr_meeseeks.py", line 4, in <module>
    import Helpers.Plugin_Handler as Plugin_Handler
File "/app/Helpers/Plugin_Handler.py", line 5, in <module>
    from Utils.constants import Plugin_Type
ImportError: No module named 'Utils.constants'

这是我的文件结构:

据我所知,Utils/constants.py 存在。如果相关,这是一个 SlackBot。其余的代码可以找到here.

Python 解释器在 $PYTHONPATH 环境变量下查找模块。看起来你或你的编辑器(我的编辑器在我将目录标记为源代码时会这样做)root 已将 SlackBot/ 添加到 $PYTHONPATH.

我自己在将目录标记为源目录时遇到了这个错误。

您有几个选择:

  • 使机器人启动脚本将 SlackBot/ 附加到 $PYTHONPATH
  • 使用相对导入 - from ..Utils import constants
  • Dynamically add SlackBot/ to the sys.path variable

此外,关于样式的注释:python 类 应该是 CamelCase,python 模块应该是 lowercase_with_underscores。如果您有像 PyCharm 这样的编辑器,您的编辑器可以自动修复这些问题,等等。

PEP 8 是官方 python 风格指南,尽管我建议使用 linter 以便自动检测和修复这些问题。

在您的 Procfile 中,将 Slackbot 附加到 PYTHONPATH,尝试添加

--pythonpath SlackBot

作为参数。 link 也解决了完全相同的问题。