cx_freeze 无法使用 Pandas 库创建 exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23094751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:56:03  来源:igfitidea点击:

cx_freeze fails to create exe with pandas library

pythonpython-2.7numpypandascx-freeze

提问by Dengar

Having problems creating exe using cx_freeze with a Pandas library. I have seen lots of others having issues with numPy but I was able to successfully bring in numPy. My big pain point has been Pandas. Is there anything in Pandas that might be causing to fail?

使用 cx_freeze 和 Pandas 库创建 exe 时遇到问题。我见过很多其他人在使用 numPy 时遇到问题,但我能够成功引入 numPy。我最大的痛点是 Pandas。Pandas 中是否有任何可能导致失败的内容?

Setup file

安装文件

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")]
    )

Code snippetshowing what I am pulling in

代码片段显示了我正在拉入的内容

import pyodbc
import numpy as np
import pandas.io.sql as psql
from pandas import  DataFrame, Series, date_range
import datetime

print("Hello World")

Here is the Error log I get

这是我得到的错误日志

> 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)
> 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)

If I remove Pandas from my Setup file and snippet and leave Numpy I have a functional executable. Anybody run into this issue? The exe gets created but none of the supporting files is added to the build directory. On open of the exe, I the program immediately crashes.

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

I am running python27 64 bit on anaconda windows 8 machine.

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

回答by Brandon W. King

Add the following to your build_exe_options:

将以下内容添加到您的 build_exe_options 中:

'build_exe': {
    'excludes': ['boto.compat.sys',
                 'boto.compat._sre',
                 'boto.compat._json',
                 'boto.compat._locale',
                 'boto.compat._struct',
                 'boto.compat.array'],
}

I looked at boto/compat.py and did not see a sys module being imported. By excluding the above list of modules, boto/compat.py is still included.

我查看了 boto/compat.py 并没有看到正在导入的 sys 模块。通过排除上面的模块列表,boto/compat.py 仍然包含在内。

After excluding 'boto.compat.sys' and 'boto.compat._sre', I got the following error:

排除“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

I ran ipythonand then typed:

我跑了ipython然后输入:

In [1]: pdb
Automatic pdb calling has been turned ON

In [2]: run setup.py build

To get access to modulefrom module.parent.path:

要访问modulemodule.parent.path

ipdb> module
<Module name='boto.compat._json', file='/Users/king/virtual_envs/py27/lib/python2.7/lib-dynload/_json.so'>

NOTE: _json.so is the built in json module. This means putting it in the includes specifically should include it. I did not since other packages caused cx_freeze to pick it up automatically. Excluding 'boto.compat._json' did the trick.

注意:_json.so 是内置的 json 模块。这意味着将它放在包含中,特别是应该包含它。我没有,因为其他软件包导致 cx_freeze 自动拾取它。排除 'boto.compat._json' 就成功了。

I repeated this until I the whole thing built. I confirmed all base modules were picked up by cx_freeze (_sre, _json, _locale, _struct, array) so I did not need to add them manually to includes.

我重复了这一点,直到我完成了整个事情。我确认所有基本模块都被 cx_freeze (_sre, _json, _locale, _struct, array) 选中,所以我不需要手动将它们添加到包含中。

So, your updated script will look like:

因此,您更新后的脚本将如下所示:

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")]
)

回答by ntg

The following should help you get over this problem (and may lead you to the next one of missing dependencies ;) )

以下内容应该可以帮助您解决这个问题(并且可能会引导您找到下一个缺少的依赖项;))

Checking the code for freeze.py, there is a case that is not checked, so I made the following changes to freezer.py:

查了freeze.py的代码,有一个case没有查,所以我对freeze.py做了如下修改:

line 600, from

第 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

to:

到:

    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