不支持按文件名导入 Gunicorn(模块)

Gunicorn Import by filename is not supported (module)

我新创建了一个容器ubuntu,并在虚拟环境中安装了需要的包。然后我通过 python path/to/my/file/X.py(在 virualenv 中)执行了预先存在的 python 服务代码,它工作正常。所以我用 gunicorn 作为 gunicorn -b 0.0.0.0:5000 path/to/my/file/X:app (在 virualenv 中)执行但我收到以下错误

2015-11-11 16:38:08 [19118] [INFO] Starting gunicorn 17.5
2015-11-11 16:38:08 [19118] [INFO] Listening at: http://0.0.0.0:444 (19118)
2015-11-11 16:38:08 [19118] [INFO] Using worker: sync
2015-11-11 16:38:08 [19123] [INFO] Booting worker with pid: 19123
2015-11-11 16:38:08 [19123] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 473, in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 100, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/dist-packages/gunicorn/app/base.py", line 115, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 33, in load
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/dist-packages/gunicorn/util.py", line 362, in import_app
    __import__(module)
ImportError: Import by filename is not supported.
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 473, in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 100, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/dist-packages/gunicorn/app/base.py", line 115, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 33, in load
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/dist-packages/gunicorn/util.py", line 362, in import_app
    __import__(module)
ImportError: Import by filename is not supported.
2015-11-11 16:38:08 [19123] [INFO] Worker exiting (pid: 19123)
2015-11-11 16:38:09 [19118] [INFO] Shutting down: Master

谁能帮我解决 ImportError: Import by filename is not supported。为什么要来?我已经在其他服务器上实现了 gunicorn,在那里工作正常。

就像错误所说的那样:您不能通过文件路径引用 Python 模块,您必须通过从 PYTHONPATH 中的目录开始的点模块路径来引用它。

gunicorn -b 0.0.0.0:5000 path.inside.virtualenv.X:app

对于 google 员工来说,当我不小心 运行 我的应用程序使用 gunicorn -paste 而不是 gunicorn --paste 时,我也遇到了这个错误。 gunicorn 没有错误,但是 api.ini 没有找到,它抛出了 'Import by filename is not supported' 错误。

我遇到了同样的问题,我使用 --chdir /path/to/project

解决了这个问题
gunicorn -b :5000 --chdir /path/to/project X:app

希望对您有所帮助