【问题标题】:cx_freeze fails to create exe with pandas librarycx_freeze 无法使用 pandas 库创建 exe
【发布时间】:2014-05-30 11:00:38
【问题描述】:

使用带有 Pandas 库的 cx_freeze 创建 exe 时遇到问题。我见过很多其他人对 numPy 有问题,但我能够成功引入 numPy。我最大的痛点是熊猫。 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")]
    )

代码 sn-p 显示我正在提取的内容

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)

如果我从我的安装文件和 sn-p 中删除 Pandas 并离开 Numpy,我有一个功能可执行文件。有人遇到这个问题吗?创建了 exe,但没有任何支持文件添加到构建目录中。打开exe后,程序立即崩溃。

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

【问题讨论】:

  • 它落在了 boto 中的某个东西上,我想它必须被 pandas 中的一些可选代码拉入。如果您不需要 boto,请尝试将其添加到 excludes 中。
  • @ThomasK 我应该对此发布更新。看来 gbq.py(Google Big Query api)是 pandas 中的问题。我将其注释掉,并且能够毫无问题地转换为 exe。
  • 我还想指出,我最终使用了 py2exe。我无法让 cx_Freeze 工作。

标签: python python-2.7 numpy pandas cx-freeze


【解决方案1】:

将以下内容添加到您的 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")]
)

【讨论】:

  • 感谢 Brandon 跟进。就像我在上面的评论中提到的那样,我最终使用了 py2exe,并从 pandas 库中排除了 GBQ、Google Big Query。您的解决方案也可能有效。
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多