【问题标题】:Cherrypy cannot find wsgiserver module when packed with cxfreezeCherrypy 与 cxfreeze 打包时找不到 wsgiserver 模块
【发布时间】:2015-07-15 20:21:30
【问题描述】:

我正在通过使用 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)

有什么提示吗?

谢谢,

通用

【问题讨论】:

    标签: python cherrypy cx-freeze


    【解决方案1】:

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

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

    更新

    这是来自文档部分 distutils commands 的引用:

    要在脚本中指定选项,请在名称中使用下划线。例如:

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

    要在命令行上指定相同的选项,请使用破折号,如下所示:

    python setup.py build_exe --init-script Console
    

    更新 2

    好的,我 pip 安装了 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'",)

    【讨论】:

    • 谢谢。如果我添加--include-modules=cherrypy.wsgiserver.wsgiserver3 命令行选项,它可以工作,但如果我将模块添加到 setup.py 中的includes 列表(请参阅我刚刚添加到我的帖子中的配置文件),则它不会工作。如何将此选项添加到我的配置文件中?
    • @gattumarrudu 看看。
    • 我尝试了完全相同的事情,但无济于事。我很惊讶这没有记录在案,所以我猜想includes 的记录和描述与include-modules 命令行选项非常相似,会产生相同的效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2022-01-24
    • 2021-08-09
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多