cx_freeze 创建 exe 文件时与 pandas 库冲突
我在用cx_freeze创建exe文件时遇到了问题,特别是和Pandas库有关。我看到很多人也在处理numPy时遇到麻烦,但我成功地把numPy引入了。我的主要问题是Pandas。Pandas里面有没有什么东西可能导致失败呢?
设置文件
from cx_Freeze import setup, Executable
build_exe_options = {
"includes": ['numpy', 'pandas'],
"packages": [],
'excludes' : [],
"include_files": []}
setup(
name = "appName",
version = "0.1",
description = "",
author = "Dengar",
options = {"build_exe": build_exe_options},
executables = [Executable("appName.py")]
)
代码片段 展示我正在引入的内容
import pyodbc
import numpy as np
import pandas.io.sql as psql
from pandas import DataFrame, Series, date_range
import datetime
print("Hello World")
这是我得到的错误日志
> Stamped: build\exe.win-amd64-2.7\appName.exe Traceback (most recent > call last): File "setup.py", line 17, in <module> > executables = [Executable("pyodbc.py")] File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\site-packages\cx_Freeze\dist.py", > line 365, in setup > distutils.core.setup(**attrs) File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\distutils\core.py", > line 152, in setup > dist.run_commands() File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\distutils\dist.py", > line 953, in run_commands > self.run_command(cmd) File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\distutils\dist.py", > line 972, in run_command > cmd_obj.run() File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\distutils\command\build.py", > line 127, in run > self.run_command(cmd_name) File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\distutils\cmd.py", > line 326, in run_command > self.distribution.run_command(command) File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\distutils\dist.py", > line 972, in run_command > cmd_obj.run() File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\site-packages\cx_Freeze\dist.py", > line 235, in run > freezer.Freeze() File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\site-packages\cx_Freeze\freezer.py", > line 582, in Freeze > self.compress, self.copyDependentFiles) File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\site-packages\cx_Freeze\freezer.py", > line 492, in _WriteModules > module.Create(finder) File "C:\Users\Dengar\AppData\Local\Continuum\Anaconda\lib\site-packages\cx_Freeze\freezer.py", > line 714, in Create > module.file, module.name) cx_Freeze.freezer.ConfigError: no file named sys (for module boto.compat.sys)
如果我把Pandas从我的设置文件和代码片段中去掉,只保留NumPy,那么我就能成功创建一个可用的exe文件。有没有人遇到过这个问题?exe文件虽然创建了,但没有任何支持文件被添加到构建目录中。打开exe时,程序立刻崩溃。
我在一个运行Python 2.7 64位的Anaconda Windows 8机器上。
2 个回答
下面的内容应该能帮助你解决这个问题(也可能会引导你遇到下一个缺少依赖的问题;)
在检查freeze.py的代码时,发现有一个情况没有被检查到,所以我对freezer.py做了以下修改:
第600行,从
try:
if module.parent is not None:
path = os.pathsep.join([origPath] + module.parent.path)
os.environ["PATH"] = path
self._CopyFile(module.file, target, copyDependentFiles)
finally:
os.environ["PATH"] = origPath
改为:
try:
if module.parent is not None:
if module.parent.path is not None:
path = os.pathsep.join([origPath] + module.parent.path)
os.environ["PATH"] = path
self._CopyFile(module.file, target, copyDependentFiles)
else:
path = os.pathsep.join([origPath, os.path.dirname(module.parent.file)])
os.environ["PATH"] = path
print '========================================================'
finally:
os.environ["PATH"] = origPath
在你的 build_exe_options 中添加以下内容:
'build_exe': {
'excludes': ['boto.compat.sys',
'boto.compat._sre',
'boto.compat._json',
'boto.compat._locale',
'boto.compat._struct',
'boto.compat.array'],
}
我查看了 boto/compat.py,发现没有导入 sys 模块。通过排除上面列出的模块,boto/compat.py 仍然被包含在内。
在排除了 'boto.compat.sys' 和 'boto.compat._sre' 后,我遇到了以下错误:
Traceback (most recent call last):
File "setup.py", line 31, in <module>
executables=executables
File "/Users/king/virtual_envs/py27/lib/python2.7/site-packages/cx_Freeze/dist.py", line 362, in setup
distutils.core.setup(**attrs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 151, in setup
dist.run_commands()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build.py", line 127, in run
self.run_command(cmd_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/Users/king/virtual_envs/py27/lib/python2.7/site-packages/cx_Freeze/dist.py", line 232, in run
freezer.Freeze()
File "/Users/king/virtual_envs/py27/lib/python2.7/site-packages/cx_Freeze/freezer.py", line 610, in Freeze
self.compress, self.copyDependentFiles)
File "/Users/king/virtual_envs/py27/lib/python2.7/site-packages/cx_Freeze/freezer.py", line 586, in _WriteModules
path = os.pathsep.join([origPath] + module.parent.path)
TypeError: can only concatenate list (not "NoneType") to list
我运行了 ipython
,然后输入了:
In [1]: pdb
Automatic pdb calling has been turned ON
In [2]: run setup.py build
要从 module.parent.path
获取 module
的访问权限:
ipdb> module
<Module name='boto.compat._json', file='/Users/king/virtual_envs/py27/lib/python2.7/lib-dynload/_json.so'>
注意:_json.so 是内置的 json 模块。这意味着将它特别放入包含列表中应该会包含它。我没有这样做,因为其他包让 cx_freeze 自动识别了它。排除 'boto.compat._json' 解决了这个问题。
我重复这个过程,直到整个构建完成。我确认所有基础模块都被 cx_freeze 识别了(_sre, _json, _locale, _struct, array),所以我不需要手动将它们添加到包含列表中。
所以,你更新后的脚本看起来会是:
from cx_Freeze import setup, Executable
build_exe_options = {
"includes": ['numpy', 'pandas'],
"packages": [],
'excludes' : ['boto.compat.sys',
'boto.compat._sre',
'boto.compat._json',
'boto.compat._locale',
'boto.compat._struct',
'boto.compat.array'],
"include_files": []}
setup(
name = "appName",
version = "0.1",
description = "",
author = "Dengar",
options = {"build_exe": build_exe_options},
executables = [Executable("appName.py")]
)