使用cx_Freeze时出现问题:“无法导入名称'tf2'”

时间:2019-08-22 12:13:33

标签: python windows tensorflow cx-freeze

我在python中有一个代码,我使用cx_Freeze将其转换为.exe。该任务有效,没有任何错误。 但是,当我尝试运行.exe时,会发生以下错误:

  从tensorflow.python

导入tf2
  ImportError:无法导入名称'tf2'

我的ann.py代码是:

import numpy as np
import sys

。 。 。

X_test=XinN
Y_test=XoutN


#Criando o modelo
from keras.models import Sequential
from keras.layers import Dense

modelo = Sequential()

for i in range(int((num_par-4)/2)):
   modelo.add(Dense(int(parametros[i+4]), kernel_initializer='normal',activation=ativacao(int(parametros[i+5])))) #camadas ocultas
modelo.add(Dense(num_out, kernel_initializer='normal',activation=ativacao(int(parametros[num_par-1])))) #camada de saída

modelo.compile(optimizer='adam',loss='mean_squared_error')

hist = modelo.fit(X_train, Y_train, epochs=800, verbose=0, batch_size=10,validation_data=(X_test, Y_test))

XobsoutN=modelo.predict(XobsN)
Xobsout=XobsoutN*(max_out-min_out)+min_out
np.savetxt("Xobsout.txt",Xobsout.transpose(),delimiter='\t')

loss=[" "," "]
loss[0] = hist.history['loss']
loss[1] = hist.history['val_loss']

np.savetxt("erro.txt",loss,delimiter='\t')

我对cx_Freeze的setyp.py是:

from cx_Freeze import setup, Executable
import sys

base = None

if sys.platform == 'win32':
    base = None


executables = [Executable("ANN.py", base=base)]

packages = ["idna"]

options = {
    'build_exe': {
        'includes':['atexit', 'numpy.core._methods', 'numpy.lib.format'],
        'packages':packages,
    },

}

import os

os.environ['TCL_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tk8.6"

setup(
    name = "Nome Executavel",
    options = options,
    version = "1.0",
    description = 'Descricao do seu arquivo',
    executables = executables
)

有人可以帮助我解决此错误吗?

使用cx_Freeze时,我还有许多其他错误,这个论坛对解决所有这些问题都非常有帮助。非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试将"tensorflow"添加到packages脚本的setup.py列表中:

 packages = ["idna", "tensorflow"]