我正在通过使用 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("没有名为 'cherrypy.wsgiserver.wsgiserver3 的模块'",)
.
关于python - Cherrypy 使用 cxfreeze 打包时找不到 wsgiserver 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31440277/