使用gunicorn运行myapp会出现错误“没有名为myapp的模块”

时间:2019-04-02 22:36:47

标签: python flask gunicorn

我有一个名为myapp的现有Flask应用,该应用使用 pyinstaller

当我使用myapp运行gunicorn时,会出现以下错误

$ gunicorn myapp:app
[2019-04-02 22:08:16 +0000] [478] [INFO] Starting gunicorn 19.9.0
[2019-04-02 22:08:16 +0000] [478] [INFO] Listening at: http://127.0.0.1:8000 (478)
[2019-04-02 22:08:16 +0000] [478] [INFO] Using worker: sync
[2019-04-02 22:08:16 +0000] [481] [INFO] Booting worker with pid: 481
[2019-04-02 22:08:16 +0000] [481] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
ImportError: No module named 'myapp' <===============
[2019-04-02 22:08:16 +0000] [481] [INFO] Worker exiting (pid: 481)
[2019-04-02 22:08:16 +0000] [478] [INFO] Shutting down: Master
[2019-04-02 22:08:16 +0000] [478] [INFO] Reason: Worker failed to boot.

myapp是通过静态链接多个python文件创建的

a = Analysis(['c.py', 'd.py', 'e.py', 'f.py', 'g.py', 'main.py'],
             pathex=['/src/myapp'],
             binaries=[],
             datas=[],
             hiddenimports=['configparser'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='myapp',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

myapp对于独立的Flask服务器而言对我来说工作得很好。现在,我需要转到gunicorn + Flask来摆脱Flask的局限性(请注意,myapp仅从其他内部服务调用,并且我不需要Web访问/ nginx / html页面等,仅需要服务于Rest调用通过myapp)

这是main.py的样子


app = Flask(__name__)

...
def runServer(args):
    app.logger.info('Starting myapp service')
    app.run('0.0.0.0', port=args.port) <=============
...

def main():
    rv = 0  # Always return 0 by default
    args = parse_args()
    if args.which == 'server':
        runServer(args)
.....

if __name__ == '__main__':
    sys.exit(main())

当我按如下所示在构建目录中运行gunicorn命令时,它运行正常

$ gunicorn main:app
[2019-04-02 22:30:55 +0000] [482] [INFO] Starting gunicorn 19.9.0
[2019-04-02 22:30:55 +0000] [482] [INFO] Listening at: http://127.0.0.1:8000 (482)
[2019-04-02 22:30:55 +0000] [482] [INFO] Using worker: sync
[2019-04-02 22:30:55 +0000] [485] [INFO] Booting worker with pid: 485

但是我希望它运行在我安装myapp的位置,而不是从构建目录内部运行

0 个答案:

没有答案