使用 cxfreeze 打包时,Cherrypy 找不到 wsgiserver 模块

Cherrypy cannot find wsgiserver module when packed with cxfreeze

我正在通过 cx_freeze 打包到一组可执行文件中来部署一个 cherrypy 应用程序。

我正在使用 python 3(通过 CentOS 中的 scl)。为了编译二进制文件,我 运行:

scl enable python33 -- cxfreeze server.py

其中 server.py 是入口脚本。

当我执行生成的文件时,服务器启动并立即停止并出现错误:

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'

cxfreeze 脚本输出包含以下行:

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

在/opt/rh/python33/root/usr/lib/python3.3/site-packages/cherrypy/wsgiserver/__init__.py中我可以看到:

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

我想知道为什么没有正确导入wsgiserver。

我还尝试在 setup.py 中明确包含 wsgiserver3 模块:

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)

有什么提示吗?

谢谢,

通用

问题包含答案。 cxfreeze 一定是在做某种依赖分析。我不知道这个方法到底是什么,例如它可以是 AST 遍历,但它正在做的是寻找 imports。当库执行动态导入时,如 exec('from .wsgiserver3 import *') cxfreeze 将无法识别它。因此,您需要在 cxfreeze 配置中明确列出此类模块。我看到配置选项调用 --include-modules.

顺便说一句,出于与动态导入 CherryPy 3.5 相同的原因(据我所知)车轮分布缺少相同的 wsgiserver3 模块。

更新

引用自文档部分 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

更新 2

好的,我 pip-installed cx_freeze(不是那么快,只是在 this comment 的帮助下)并自己做了测试。

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
)

它确实适用于 {'includes': 'cherrypy.wsgiserver.wsgiserver3'},但没有它,ImportError("No module named 'cherrypy.wsgiserver.wsgiserver3'",)