【问题标题】:Why does cx_Freeze rise this error when running an exe file?为什么 cx_Freeze 在运行 exe 文件时会出现此错误?
【发布时间】:2019-02-08 10:56:10
【问题描述】:

我是 cx_Freeze 的新用户,我想将我的 python 文件与所有需要的依赖项打包在一起。

我使用 Python3

我在一个带有 tensorflow, sklearn, matplotlib, python-vlc... 等依赖项的 python virtualenv 下工作

以下是在 Windows 上重现相同错误所需的所有文件。

我的requirements.txt要安装

absl-py==0.7.0
altgraph==0.16.1
astor==0.7.1
audioread==2.1.6
certifi==2018.11.29
chardet==3.0.4
cx-Freeze==5.1.1
cycler==0.10.0
decorator==4.3.2
distlib==0.2.8
future==0.17.1
gast==0.2.2
grpcio==1.18.0
h5py==2.9.0
idna==2.8
Jinja2==2.10
joblib==0.13.1
Keras-Applications==1.0.7
Keras-Preprocessing==1.0.9
kiwisolver==1.0.1
librosa==0.6.2
llvmlite==0.27.0
macholib==1.11
Markdown==3.0.1
MarkupSafe==1.1.0
matplotlib==3.0.2
numba==0.42.0
numpy==1.16.1
pefile==2018.8.8
protobuf==3.6.1
py2exe==0.9.2.2
PyInstaller==3.4
pynsist==2.3
pyparsing==2.3.1
pypiwin32==223
pysrt==1.1.1
python-dateutil==2.8.0
python-vlc==3.0.4106
pywin32==224
pywin32-ctypes==0.2.0
requests==2.21.0
requests-download==0.1.2
resampy==0.2.1
scikit-learn==0.20.2
scipy==1.2.0
six==1.12.0
sklearn==0.0
tensorboard==1.12.2
tensorflow==1.12.0
termcolor==1.1.0
tornado==5.1.1
urllib3==1.24.1
watson-developer-cloud==2.8.0
websocket-client==0.48.0
Werkzeug==0.14.1
yarg==0.1.9

我有一个主要的 python 文件 IHM.py,它从另外两个 python 文件 sync.pyneurnet.py 导入模块。

文件ihm.py

# -*- coding: utf-8 -*-

# for Python3
from tkinter import *
import matplotlib.backends.backend_tkagg
import matplotlib.pyplot as mp
import tkinter.filedialog
from watson_developer_cloud import LanguageTranslatorV3
import json
import subprocess
import os
import vlc
from sync import *
import warnings

warnings.filterwarnings("ignore")

# Définition des variables globales
vid_file_path = ""
vid_srt_path = ""
vid_srt_path_trans = ""
vid_srt_path_sync = ""


# Création de la fenetre d'IHM
fenetre = Tk()
fenetre.title("Test Tkinter Windows")



fenetre.mainloop()

文件sync.py

# -*- coding: utf-8 -*-
from __future__ import division
from neuralNet import 

文件 neurnet.py

#coding: utf-8
import os
import time
import sys
import librosa
import re
import io
import subprocess
import pysrt
import numpy as np
import matplotlib.pyplot as plt
from time import time
import tensorflow as tf
import pickle
#from sklearn.model_selection import train_test_split
#from tensorflow.contrib.layers import flatten
import sklearn



from subprocess import STDOUT

try:
    from subprocess import DEVNULL # py3k
except ImportError:
    import os
    DEVNULL = open(os.devnull, 'wb')

这里是 cx_Freeze setup.py

# setup.py 
import sys, os
from cx_Freeze import setup, Executable

os.environ['TCL_LIBRARY'] = 'C:/Program Files/Python36/tcl/tcl8.6' 
os.environ['TK_LIBRARY'] = 'C:/Program Files/Python36/tcl/tk8.6'
__version__ = "1.1.0"

buildOptions = dict(
    packages = [],
    excludes = [],  includes = ["idna.idnadata"],
    include_files = ['C:/Program Files/Python36/DLLs/tcl86t.dll','C:/Program Files/Python36/DLLs/tk86t.dll'] )

import sys

base = 'Win32GUI' if sys.platform=='win32' else None 
executables = [
    Executable('ihm.py', base=base) 
] 
setup(
    name = "mgp320",
    description='Projet Neural Network Speech Detection',
    version=__version__,
    options = dict(build_exe = buildOptions),
    executables = executables
)

使用命令运行此设置:python setup.py build 所以它为我创建了一个 exe 文件,但是当我运行这个 exe 时,我得到了这个错误窗口:

Error cx_Freeze

引用如下:

Traceback (most recent call last):
 File
"C:\Users\achraf.bentabib\Desktop\aapsa\aapsa\env\lib\site-p
ackages\cx_Freeze\initscript\__startup__.py", line 14, in run
  module.run()
 File
"C:\Users\achraf.bentabib\Desktop\aapsa\aapsa\env\lib\site-p
ackages\cx_Freeze\initscript\Console.py", line 26, in run
  exec(code, m.__dict__)
 File "ihm.py", line 5, in <module>
 File
"C:\Users\achraf.bentabib\Desktop\aapsa\aapsa\env\lib\site-p
ackages\matplotlib\__init__.py", line 120, in <module>
   import distutils.version
 File
"C:\Users\achraf.bentabib\Desktop\aapsa\aapsa\env\lib\distut
ils\__init__.py", line 35, in <module>
   loader.exec_module(real_distutils)
 File "<frozen importlib._bootstrap_external>", line 674, in
exec_module
 File "<frozen importlib._bootstrap_external>", line 780, in
get_code
 File "<frozen importlib._bootstrap_external>", line 832, in
get_data
FileNotFoundError: [Errno 2] No such file or directory:
"C:\\Users\\achraf.bentabib\\Desktop\\aapsa\\aapsa\\stack\\b
uild\\exe.win-amd64-3.6\\lib\\library.zip\\distutils\\__init__.py'

我真的不知道我该如何解决这个问题..

【问题讨论】:

标签: python cx-freeze distutils


【解决方案1】:

我在cx_Freeze 5.1.1 的设置脚本中发现了以下潜在问题:

  1. 您正在使用numpy(和matplotlib,这取决于numpy)。为了使cx_Freeze 正确冻结numpy,需要将其添加到build_exe 选项的packages 列表中。

  2. 对于cx_Freeze 5.1.1 版,TCL/TK DLL 需要包含在构建目录的lib 子目录中。您可以通过将元组 (source, destination) 传递给 include_files 列表选项的相应条目来做到这一点。此外,动态查找 TCL/TK DLL 的位置会更安全。

总之,尝试在您的设置脚本中进行以下修改:

PYTHON_INSTALL_DIR = os.path.dirname(sys.executable)
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

buildOptions = dict(
    packages = ["numpy"],
    excludes = [],
    includes = ["idna.idnadata"],
    include_files=[(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
                    os.path.join('lib', 'tk86t.dll')),
                   (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
                    os.path.join('lib', 'tcl86t.dll'))]
)

如果此修改仍然无法正常工作,请从最小的tkinter 应用程序开始,例如通过在您发布的示例应用程序中注释掉除tkinter 之外的所有导入并使其在您的系统上运行。然后逐个重新添加您需要的模块(numpymatplotlib,...),并检查解冻和冻结的应用程序在每个步骤中是否正常工作。例如,您可以在示例应用程序中添加一个消息框,并在那里打印您导入的每个模块的版本。

【讨论】:

  • 感谢您的回复。它仍然不起作用。但是我已经修复了除“librosa”之外的所有导入的错误。 Cx_freeze 引发有关“distutils”包的错误。我尝试使用 buildOptions 的 'zip_include_packages' 参数手动将 i 添加到 library.zip ...但仍然不起作用
  • @AchrafBentabib 至少看起来我们越来越近了。尝试将“librosa”和“audioread”添加到packageslist。根据文档,librosa 需要 scipy,它本身需要对 cx_Freeze 进行额外调整:将“scipy”添加到packages 列表,将“scipy.spatial.cKDTree”添加到excludes 列表。
  • @AchrafBentabib 如果这仍然不起作用,请发布新错误的完整堆栈跟踪。
  • 感谢@jpeg,我按照你说的修改了,但仍然无法正常工作。请参阅下面的新堆栈跟踪
【解决方案2】:

我终于找到了上次回溯的解决方案:

from distutils import dist, sysconfig # isort:skip 
ImportError: cannot import name "dist" 

问题是distutils 没有很多未安装在 virtualenv 中的模块。 (仅限__init__.py) 因此,当我们构建 exe 时,它​​没有找到 distutils 模块...

要解决这个问题,我们必须手动导入 disutils

import distutils
import opcode
import os
distutils_path = os.path.join(os.path.dirname(opcode.__file__), 'distutils')

然后,将此路径包含到 cx_freeze 的 include_files 选项中,然后 在排除部分排除 distutils。

buildOptions = dict(
    packages = ['llvmlite', 'pkg_resources._vendor', "tkinter", 'numba', "tkinter.filedialog", "audioread", "librosa", "scipy", "numpy"],
    excludes = ["scipy.spatial.cKDTree", 'distutils'],
    includes = ["idna.idnadata", 'numpy.core._methods', 'numpy.lib.format', 'matplotlib.backends.backend_tkagg'],
    include_files = [(distutils_path, 'distutils'), 'C:/Program Files/Python36/DLLs/tcl86t.dll','C:/Program Files/Python36/DLLs/tk86t.dll']
)

我还在包中添加了pkg_resources._vendor,因为没有这个,cx_freeze 会引发类似

的错误
ImportError: The 'appdirs' package is required; normally this is bundled with th
is package so if you get this warning, consult the packager of your distribution

终于成功了,我将numba添加到包含的包中,因为与 disutils 一样,build 不会创建所有必需的模块(对于 librosa)。

最后一个问题是关于multiprocessing lib。 我必须手动将 build lib 文件夹中的 Pool.pyc 重命名为 pool.pyc。

【讨论】:

  • 你是否设法用 tensorflow 冻结而没有错误?
  • @user13640482 是的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 2017-08-08
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多