简体   繁体   English

heroku上的gunicorn导入错误

[英]gunicorn import error on heroku

I cannot run gunicorn on heroku with simple flask app the application is really simple, this app.py 我不能用简单的烧瓶应用程序在heroku上运行gunicorn应用程序非常简单,这个app.py

app = Flask(__name__)

@app.route("/")
def say_hello(url):
    return "Hello"


if __name__ == "__main__":
    port = int(os.environ.get('PORT', 8888))
    app.run(host='0.0.0.0',port=port)

the app works fine through flask test server on heroku when i switch to use gunicorn it crashs with: 当我切换到使用gunicorn它崩溃时,应用程序在heroku上烧瓶测试服务器正常工作:

ImportError: No module named app.wsgiapp

my requirements.txt: 我的requirements.txt:

Flask==0.8
gevent==0.13.7
gunicorn==0.13.2

i tried different versions of gunicorn from 0.13.7 to 0.14.6 with no success 我试过不同版本的gunicorn从0.13.7到0.14.6没有成功

Procfile: Procfile:

web: gunicorn app:app -w 4 -b 0.0.0.0:$PORT

running command: 运行命令:

heroku logs

gives

←[33m2012-08-09T21:08:02+00:00 app[web.1]:←[0m ImportError: No module named app.
wsgiapp ←[33m2012-08-09T21:08:02+00:00 app[web.1]:←[0m     entry = __import__(self.modul
e_name, globals(),globals(), ['__name__'])

Any help please 请帮忙

Thanks 谢谢

Joe

In my case, I got this error by having a gunicorn.py file in my top level folder. 在我的情况下,我通过在我的顶级文件夹中有一个gunicorn.py文件来解决此错误。 This clashed with the installed gunicorn library on Heroku. 这与Heroku上安装的gunicorn库发生了冲突。

So my run command that caused the problem was: 所以导致问题的运行命令是:

gunicorn -c gunicorn.py myapp:main

Raising the following error: 引发以下错误:

Traceback (most recent call last):
  File "/app/.heroku/python/bin/gunicorn", line 9, in <module>
    load_entry_point('gunicorn==18.0', 'console_scripts', 'gunicorn')()
  File "/app/.heroku/python/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/app/.heroku/python/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point
    return ep.load()
  File "/app/.heroku/python/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named app.wsgiapp

Whereas after a mv gunicorn.py gunicorn_config.py it worked fine with: 而在一个mv gunicorn.py gunicorn_config.py它运行良好:

gunicorn -c gunicorn_config.py myapp:main

I ran into this problem when upgrading Ubuntu to 14.04 LTS. 我在将Ubuntu升级到14.04 LTS时遇到了这个问题。

For some reason, gunicorn failed to pick up the correct python path to resolve the wsgi module. 出于某种原因, gunicorn未能找到正确的python路径来解析wsgi模块。

I resolved this, for now, by declaring the python path explicitly to gunicorn via the --pythonpath parameter (documented here ). 我现在通过--pythonpath参数( 在此处记录 )将python路径明确声明为gunicorn来解决这个问题。

For example: 例如:

gunicorn --pythonpath /path/to/containing/directory "app.wsgi_app:wsgi_app"

I finally figured this one out. 我终于想出了这一个。

It's basically just a PATH problem. 它基本上只是一个PATH问题。 If you import certain modules (like os and sys ) in the wrong order depending on your setup, you will cause Gunicorn to look in the wrong package for the app.wsgiapp module. 如果你根据你的设置以错误的顺序导入某些模块(如ossys ),你将导致Gunicornapp.wsgiapp模块中查找错误的包。 (not to be confused with the app.wsgi_app function in Flask) (不要与Flask中的app.wsgi_app函数混淆)

The correct import order will vary depending on your setup, but the rule of thumb based on what I was able to get working was to just make sure that your sys module is imported before your os module. 正确的导入顺序将根据您的设置而有所不同,但基于我能够工作的经验法则是确保在您的os模块之前导入您的sys模块。

Beyond that, assuming the rest of the config is normal (as above) you shouldn't have any issues. 除此之外,假设配置的其余部分是正常的(如上所述),您不应该有任何问题。

Note: THIS IS ONLY A PROBLEM ON HEROKU with Gunicorn. 注意:这只是一个与Gunicorn的HEROKU问题。 It has something to do with how their PYTHONPATH and module search path is set up. 它与他们的PYTHONPATH和模块搜索路径的设置方式有关。 I don't know why exactly, but this is only necessary for the production environment, local setups will work fine regardless of the module import order. 我不知道原因究竟是什么,但这只是生产环境所必需的,无论模块导入顺序如何,本地设置都能正常工作。

My guess is there is an other "app" module in the python path (both gunicorn and flask have a module called app already). 我的猜测是python路径中还有一个“app”模块(gunicorn和flask都有一个名为app的模块)。 Rename it anything else than app.py and it should work. 重命名除app.py以外的任何东西,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM