为什么cx_Freeze运行的exe文件时上升这个错误?

问题描述 投票:2回答:2

我cx_Freeze的新用户,我想我的打包文件的python的所有所需的依赖。

我用Python3。

我一个python的virtualenv下工作与相关性,如tensorflow, sklearn, matplotlib, python-vlc...

下面是重现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.pysync.py一个主要的Python文件neurnet.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
2个回答
3
投票

我看到你的安装脚本cx_Freeze 5.1.1以下潜在的问题:

  1. 您正在使用numpy(和matplotlib取决于numpy)。为了使cx_Freeze正确冻结numpy,它需要被添加到的packages选项build_exe列表。
  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,...)模块和检查解冻和冷冻应用在每一步工作。例如,你可以一个消息框添加到示例应用程序和打印有每次导入模块的版本。


1
投票

我终于找到了最后的追踪的解决方案:

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

问题是,distutils没有未安装在virtualenv中的许多模块。 (仅__init__.py)所以,当我们所建立的EXE,它没有找到的distutils模块...

为了解决这个问题,我们必须手工导入的distutils

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

最后它的工作,我加qazxsw poito的包包括因为像的distutils,构建不会创建所需的所有模块(librosa)。

最后一个问题是关于numba库。我必须手动重命名multiprocessing在构建lib文件夹到Pool.pycc。

© www.soinside.com 2019 - 2024. All rights reserved.