Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name NullHandler

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

Link to concurrent AWS Forum Post.

like image 953
Erik Avatar asked Mar 01 '26 10:03

Erik


2 Answers

I had similar problem and in my case the issue was that for some unrelated project I created logging.py file in home folder and when I ran something in home, it was importing this file instead of the real module.

You can check which file is being imported like this:

import logging
print(logging.__file__)

I fixed it by deleting logging.py I created previously.

like image 182
Filip Dušek Avatar answered Mar 03 '26 00:03

Filip Dušek


The NullHandler is only available on Python version 2.7+. You could create the NullHandler yourself on an ImportError:

import logging

try:
    from logging import NullHandler
except ImportError:
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass

logging.getLogger(__name__).addHandler(NullHandler())

More information about logging.NullHandler: https://docs.python.org/3/library/logging.handlers.html#logging.NullHandler.

like image 24
Roald Nefs Avatar answered Mar 03 '26 00:03

Roald Nefs