Search code examples
pythoncherrypycx-freeze

Cherrypy cannot find wsgiserver module when packed with cxfreeze


I am deploying a cherrypy app by packing into a set of executable files with cx_freeze.

I am using python 3 (via scl in CentOS). In order to compile the binaries I run:

scl enable python33 -- cxfreeze server.py

where server.py is the entry script.

When I execute the resulting file, the server starts and immediately stops with the error:

Traceback (most recent call last):
  File "/opt/rh/python33/root/usr/lib/python3.3/site-packages/cherrypy/process/wspbus.py", line 205, in publish
    output.append(listener(*args, **kwargs))
  File "/opt/rh/python33/root/usr/lib/python3.3/site-packages/cherrypy/_cpserver.py", line 167, in start
    self.httpserver, self.bind_addr = self.httpserver_from_self()
  File "/opt/rh/python33/root/usr/lib/python3.3/site-packages/cherrypy/_cpserver.py", line 157, in httpserver_from_self
    from cherrypy import _cpwsgi_server
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1616, in _handle_fromlist
    _call_with_frames_removed(import_, from_name)
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 313, in _call_with_frames_removed
    return f(*args, **kwds)
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1567, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1534, in _find_and_load_unlocked
    loader.load_module(name)
  File "/opt/rh/python33/root/usr/lib/python3.3/site-packages/cherrypy/_cpwsgi_server.py", line 7, in <module>
    from cherrypy import wsgiserver
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1616, in _handle_fromlist
    _call_with_frames_removed(import_, from_name)
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 313, in _call_with_frames_removed
    return f(*args, **kwds)
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1567, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1534, in _find_and_load_unlocked
    loader.load_module(name)
  File "/opt/rh/python33/root/usr/lib/python3.3/site-packages/cherrypy/wsgiserver/__init__.py", line 14, in <module>
    exec('from .wsgiserver3 import *')
  File "<string>", line 1, in <module>
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1567, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/_bootstrap.py", line 1531, in _find_and_load_unlocked
    raise exc
ImportError: No module named 'cherrypy.wsgiserver.wsgiserver3'

The cxfreeze script output contains, among the rest, this line:

Missing modules:
[...]
? wsgiserver2 imported from cherrypy.wsgiserver
[...]
This is not necessarily a problem - the modules may not be needed on this platform.

In /opt/rh/python33/root/usr/lib/python3.3/site-packages/cherrypy/wsgiserver/__init__.py I can see:

import sys
if sys.version_info < (3, 0):
    from wsgiserver2 import *
else:
    # Le sigh. Boo for backward-incompatible syntax.
    exec('from .wsgiserver3 import *')

I wonder why wsgiserver is not imported correctly.

I also tried to include the wsgiserver3 module explicitly in setup.py:

buildOptions = {
    'packages' : ['cherrypy'],
    'includes' : ['cherrypy.wsgiserver.wsgiserver3'],
    'excludes' : [],
    'path' : sys.path,
}

import sys
base = 'Win32Service' if sys.platform=='win32' else None

executables = [
    Executable('server.py', base=base, targetName = 'myapp')
]

setup(name='Myapp',
      version = '1.0beta1',
      description = 'My App',
      options = dict(build_exe = buildOptions),
      executables = executables)

Any hints?

Thanks,

gm


Solution

  • The question included the answer. cxfreeze must be doing some sort of dependency analysis. I don't know exactly what the method is, it can be an AST traversal for instance, but what it is doing is looking for imports. When a library does a dynamic import like exec('from .wsgiserver3 import *') cxfreeze won't recognize it. Thus you need to list such modules explicitly in cxfreeze configuration. I see configuration option call --include-modules.

    Btw, for the same reason of dynamic import CherryPy 3.5 (as far as I can remember) wheel distribution lacked the same wsgiserver3 module.

    Update

    Here's a quote from documentation section distutils commands:

    To specify options in the script, use underscores in the name. For example:

    setup(options = {'build_exe': {'init_script':'Console'}} )
    

    To specify the same options on the command line, use dashes, like this:

    python setup.py build_exe --init-script Console
    

    Update 2

    Okay, I pip-installed cx_freeze (not that fast, only with help of this comment) and did the test myself.

    app.py

    #!/usr/bin/env python3
    
    
    import cherrypy
    
    
    class App:
    
      @cherrypy.expose
      def index(self):
        return 'Hello world!'
    
    
    if __name__ == '__main__':
      cherrypy.quickstart(App(), '/')
    

    setup.py

    import sys
    
    from cx_Freeze import setup, Executable
    
    
    options = {
      'build_exe' : {
        'includes' : 'cherrypy.wsgiserver.wsgiserver3'
      }
    }
    executables = [Executable('app.py')]
    
    setup(
      name        = 'CherryPyApp',
      version     = '0.1',
      description = 'Testing CherryPy wsgiserver3 dynamic import',
      options     = options,
      executables = executables
    )
    

    It does work with {'includes': 'cherrypy.wsgiserver.wsgiserver3'}, it doesn't without it, ImportError("No module named 'cherrypy.wsgiserver.wsgiserver3'",).