如何在本地测试Heroku python应用程序? ImportError:没有名为wsgi的模块

时间:2016-12-01 14:15:05

标签: python heroku flask wsgi

首次在这里使用Heroku。我想主持一个python Flask应用程序。有点开始了解事情的运作方式。 我想使用StatisticsId = (from s in z.Statistics where s.ZoneId == 100 orderby s.Flag, s.Emergency descending, s.Date descending select (int?)s.Id).FirstOrDefault() ?? 0 命令在本地测试我的应用程序,但是我收到此错误:

  

ImportError:没有名为wsgi的模块

以下是我的文件:

Procfile:

heroku local

myapp.wsgi:

web: gunicorn choposcope.wsgi --log-file -

错误堆栈跟踪:

from choposcope import app as application

另外,我不知道wsgi模块是否应该默认安装在flask中。当我尝试在虚拟环境中运行[2016-12-01 18:36:21 +0100] [9916] [INFO] Starting gunicorn 19.6.0 6:36:21 PM web.1 | [2016-12-01 18:36:21 +0100] [9916] [INFO] Listening at: http://0.0.0.0:5000 (9916) 6:36:21 PM web.1 | [2016-12-01 18:36:21 +0100] [9916] [INFO] Using worker: sync 6:36:21 PM web.1 | [2016-12-01 18:36:21 +0100] [9919] [INFO] Booting worker with pid: 9919 6:36:34 PM web.1 | [2016-12-01 18:36:34 +0100] [9919] [ERROR] Exception in worker process 6:36:34 PM web.1 | Traceback (most recent call last): 6:36:34 PM web.1 | File "/Users/Yann/Documents/dev/choposcope/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker 6:36:34 PM web.1 | worker.init_process() 6:36:34 PM web.1 | File "/Users/Yann/Documents/dev/choposcope/env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process 6:36:34 PM web.1 | self.load_wsgi() 6:36:34 PM web.1 | File "/Users/Yann/Documents/dev/choposcope/env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi 6:36:34 PM web.1 | self.wsgi = self.app.wsgi() 6:36:34 PM web.1 | File "/Users/Yann/Documents/dev/choposcope/env/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi 6:36:34 PM web.1 | self.callable = self.load() 6:36:34 PM web.1 | File "/Users/Yann/Documents/dev/choposcope/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load 6:36:34 PM web.1 | return self.load_wsgiapp() 6:36:34 PM web.1 | File "/Users/Yann/Documents/dev/choposcope/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp 6:36:34 PM web.1 | return util.import_app(self.app_uri) 6:36:34 PM web.1 | File "/Users/Yann/Documents/dev/choposcope/env/lib/python2.7/site-packages/gunicorn/util.py", line 357, in import_app 6:36:34 PM web.1 | __import__(module) 6:36:34 PM web.1 | ImportError: No module named wsgi 6:36:34 PM web.1 | [2016-12-01 18:36:34 +0100] [9919] [INFO] Worker exiting (pid: 9919) 6:36:34 PM web.1 | [2016-12-01 18:36:34 +0100] [9916] [INFO] Shutting down: Master 6:36:34 PM web.1 | [2016-12-01 18:36:34 +0100] [9916] [INFO] Reason: Worker failed to boot. [DONE] Killing all processes with signal null 6:36:34 PM web.1 Exited with exit code 3 时,出现此错误:

pip install mod_wsgi

2 个答案:

答案 0 :(得分:2)

从你给出的命令行,gunicorn希望你有一个名为:

的文件
choposcope/wsgi.py

您没有显示目录和文件的设置结构,但错误表明此类文件不存在。

虽然有一种方法可以在Heroku上安装mod_wsgi-express,但我不建议使用它,因为不支持该机制。

如果您打算使用gunicorn进行本地开发以匹配Heroku上使用的内容,那么您也不需要在那里安装mod_wsgi-express,尽管如果您没有理由不能使用mod_wsgi-express进行开发希望mod_wsgi-express(非手动Apache / mod_wsgi配置)与其他可用选项一样容易进行本地开发。

我不确切知道您正在使用哪种文件系统结构,我猜你需要做的是将文件myapp.wsgi重命名为wsgi.py。然后将Procfile更改为:

web: gunicorn wsgi --log-file -

wsgi参数是Python模块名称,因此需要调用文件wsgi.py的原因。使用gunicorn时,它希望WSGI应用程序入口点默认为application。你可能也没有使用wsgi.py文件并使用了:

web: gunicorn choposcope:app --log-file -

至于为什么pip install mod_wsgi在尝试在本地安装时失败,这可能是由于在MacOS X上并且没有安装Xcode开发人员命令行工具。那个或者你在HomeBrew中安装了一个破碎或不完整的Apache httpd。

答案 1 :(得分:1)

您不必下载它,您必须创建它。

文件的内容必须是:

from yourapplication import app as application

有关详细信息,请参阅Flask文档:http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/#creating-a-wsgi-file