python - cx_freeze 无法使用 pandas 库创建 exe

标签 python python-2.7 numpy pandas cx-freeze

使用带有 Pandas 库的 cx_freeze 创建 exe 时遇到问题。我看到很多其他人在使用 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,程序立即崩溃。

我在 anaconda windows 8 机器上运行 python27 64 位。

最佳答案

将以下内容添加到您的 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 模块。这意味着将它放在 includes 中应该特别包含它。我没有,因为其他包导致 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")]
)

关于python - cx_freeze 无法使用 pandas 库创建 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23094751/

相关文章:

python - ImportError : No module named api_core. 异常

python - 如何确定带有队列的Python线程是否完成了任务?

python - 多次连续加入pyspark

python - 保持卡住集中元素的顺序

python - 不使用切片的 Numpy 索引

python - 如何在 PUT 方法中给出 2 个参数?

python - 如何从包含 17 个元素的列表到更多元素

python - 如何删除 etree python 中的元素?

python - 有效地计算 numpy 数组中的排序排列

python - 欧氏距离的迭代计算