【问题标题】:Gunicorn ImportError: No module name app only when directory name is siteGunicorn ImportError:仅当目录名称为站点时才没有模块名称应用程序
【发布时间】:2017-05-20 02:20:36
【问题描述】:

尝试使用 hello world 应用运行 gunicorn。
这是我的项目目录结构

Project
  |_ config
  |  |_ ___init__.py
  |  |_ settings.py
  |
  |_ instance
  |  |_settings.py_production
  |
  |_ site
  |  |_ __init__.py
  |  |_ app.py
  |
  |_ .env
  |_ docker-compose.yml
  |_ Dockerfile
  |_ requirements.txt

我在 docker 容器中运行它,但是当我以交互模式进入容器时,会发生相同的行为。

当目录命名为site 并运行命令gunicorn -b 0.0.0.0:8000 --access-logfile - "site.app:create_app()" 时,我收到错误ImportError: No module name app。但是,如果我将目录命名为 foosnakeeyesblah,我不会收到错误消息,并且应用程序会按预期运行。是否有一个原因?目录不能命名为站点吗? python中是否还有其他限制目录名称?

【问题讨论】:

  • 你不应该把你的模块称为 Python 标准库中的任何模块,因为这取决于模块导入路径的顺序,你会得到错误的。

标签: python flask gunicorn


【解决方案1】:

site 是 python 中standard library 的一部分。我假设,但不是肯定的,你正在经历一场冲突。

site 不是 python 中的保留字。如果您对标准关键字感兴趣,可以查看此 SO 帖子:Is the list of Python reserved words and builtins available in a library?

我认为您可以按照文档的指示通过抑制库的自动导入来测试这是否是冲突:

This module is automatically imported during initialization. The automatic import can be suppressed using the interpreter’s -S option.

【讨论】:

  • running gunicorn -S -b 0.0.0.0:8000 --access-logfile - "pickle.app:create_app()" 抛出错误,指出 -S 不是有效选项。但是,我确实在站点所在的改进模块下查看了docs.python.org/3/contents.html 的列表。我将所有内容重命名为另一个模块 pickle 并得到相同的 ImportError: No module named app 结果。我相信这是正确的答案。
  • Gunicorn 不是 python 的解释器。该文档指的是对 python 的实际调用。所以,像python -S 这样的东西。如果你想尝试这个实验,你需要弄清楚如何通过 gunicorn 将命令行参数传递给 python 解释器。
猜你喜欢
  • 2017-06-30
  • 2018-03-10
  • 2018-09-04
  • 2018-10-23
  • 2019-12-01
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
相关资源
最近更新 更多