gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:42:45 24 4
gpt4 key购买 nike

我正在通过使用 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 遍历,但它正在做的是寻找 import。当库执行动态导入时,如 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 安装了 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 '",).

关于python - 使用 cxfreeze 打包时,Cherrypy 找不到 wsgiserver 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31440277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com