ImportError:无法导入名称NullHandler

问题描述:

我正在尝试在Elastic Beanstalk上部署一个hello-world类型的应用程序.几乎所有事情似乎都可以正常工作,已经安装了软件包,等等.直到mod_wsgi尝试从wsgi.py检索应用程序"对象为止.此时,以下内容会出现在日志中(对于每个失败的HTTP请求,在日志中都会出现以下内容):

I'm trying to deploy a hello-world type app on Elastic Beanstalk. Just about everything seems to work, packages are installed, etc. up to the point where mod_wsgi attempts to retrieve the "application" object from wsgi.py. At that point, the following appears in the logs (once in the logs for each unsuccessfuly HTTP request):

mod_wsgi (pid=6114): Target WSGI script '/opt/python/current/app/myapp/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=6114): Exception occurred processing WSGI script '/opt/python/current/app/myapp/wsgi.py'.
Traceback (most recent call last):
   File "/opt/python/current/app/caserails/wsgi.py", line 20, in <module>
     application = get_wsgi_application()
   File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
     django.setup()
   File "/opt/python/run/venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
     from django.utils.log import configure_logging
   File "/opt/python/run/venv/lib/python2.7/site-packages/django/utils/log.py", line 16, in <module>
     from logging import NullHandler  # NOQA
 ImportError: cannot import name NullHandler

链接到并发的AWS论坛帖子.

经过反复试验,通过删除python 记录并重建环境.

After much trial and error, the (immediate) problem was solved by removing python logging from requirements.txt and rebuilding the environment.

我还不完全理解为什么这是一个问题.在我的本地计算机上,我能够安装日志记录并运行Django而不会出现错误.正如vikramls指出的那样,我怀疑在mod_wsgi,执行mod_wsgi的baselineenv和运行我的应用程序的virtualenv之间的交集中发生了奇怪的事情.

I do not yet fully understand why this is a problem. On my local machine, I'm able to install logging and run Django without error. I suspect as vikramls pointed out that something weird is happening in the intersection between mod_wsgi, the baselineenv in which mod_wsgi executes, and the virtualenv in which my app operates.

但是,至少到目前为止,我正在通过在Elastic Beanstalk上部署Django 1.7时不在"requests.txt"中包括"logging"来解决此错误.

But, at least for now, I'm fixing this error by not including "logging" in requirements.txt when deploying Django 1.7 on Elastic Beanstalk.