Cherrypy - ImportError: No module named 'winreg'

When starting up Cherrypy (on Ubuntu 14.04, using Python 3) I get the following error (shown in my Python console):

[04/Sep/2014:22:14:20] ENGINE Started monitor thread '_TimeoutMonitor'.
    [04/Sep/2014:22:14:20] ENGINE Started monitor thread 'Autoreloader'.
    [04/Sep/2014:22:14:20] ENGINE Serving on http://0.0.0.0:50006
    [04/Sep/2014:22:14:20] ENGINE Bus STARTED
    [04/Sep/2014:22:14:21] ENGINE Error in background task thread function <bound method Autoreloader.run of 
<cherrypy.process.plugins.Autoreloader object at 0x7fe28413e208>>.
    Traceback (most recent call last):
      File "/usr/local/lib/python3.4/dist-packages/cherrypy/process/plugins.py", line 500, in run
        self.function(*self.args, **self.kwargs)
      File "/usr/local/lib/python3.4/dist-packages/cherrypy/process/plugins.py", line 632, in run
        for filename in self.sysfiles() | self.files:
      File "/usr/local/lib/python3.4/dist-packages/cherrypy/process/plugins.py", line 616, in sysfiles
        hasattr(m, '__loader__') and
      File "/usr/lib/python3/dist-packages/six.py", line 116, in __getattr__
        _module = self._resolve()
      File "/usr/lib/python3/dist-packages/six.py", line 105, in _resolve
        return _import_module(self.mod)
      File "/usr/lib/python3/dist-packages/six.py", line 76, in _import_module
        __import__(name)
    ImportError: No module named 'winreg'

Since Winreg is a Windows-specific module (used for accessing the registry), Six (a Python 2 to 3 compatibility module) probably shouldn't try to load it under Linux.

Looking in six.py, I see the following:

def __getattr__(self, attr):
        # Hack around the Django autoreloader. The reloader tries to get
        # __file__ or __name__ of every module in sys.modules. This doesn't work
        # well if this MovedModule is for an module that is unavailable on this
        # machine (like winreg on Unix systems). Thus, we pretend __file__ and
        # __name__ don't exist if the module hasn't been loaded yet. See issues
        # #51 and #53.
        if attr in ("__file__", "__name__") and self.mod not in sys.modules:
            raise AttributeError
        _module = self._resolve()
        value = getattr(_module, attr)
        setattr(self, attr, value)
        return value

From the comment above: This doesn't work well if this MovedModule is for a module that is unavailable on this machine (like winreg on Unix systems)

Since the stack trace indicates that this exception is occurring in the AutoReloader functionality, and I don't need that functionality, I just disabled it by adding:

'engine.autoreload_on': False

as follows:

from controllers import MyController
import cherrypy

if __name__ == '__main__':
    server_config = {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 60000,
        'engine.autoreload_on': False
    }

    cherrypy.tree.mount(MyController(), '/Home', {})
    cherrypy.config.update(server_config)
    cherrypy.engine.start()

and now the exception is not thrown.

Comments

Leave a comment

What color are green eyes? (spam prevention)
Submit
Code under MIT License unless otherwise indicated.
© 2020, Downranked, LLC.