0

I did a program who perfectly worked when executing it via Python, but when I compute the executable, it says ImportError: No module named 'tkinter'. I do not understand why because everything seems to be okay to me.

I paste a copy of my app.py into the C:\Python\Scripts folder where pyInstaller is. I compute the command line pyinstaller -F app.py, this creates a app.exe in the dist folder. When I run this code I have

ImportError: No module named 'tkinter'
Failed to execute script prog

I am block for days because of trying to make an .exe :(

OS : Windows64

PyInstaller installed via pip install

Python : 3.5 for Win64

Note : I already unistall/install python 3 times today (after reading documentation on this webside and abroad).

Note 2 : I use Python only for scientific issues. I am no computer scientist, so be kind to me when explaining computer stuff :S

Here my code :

from math import*
from pylab import*
import numpy as np
from tkinter import *
from tkinter.messagebox import *

import matplotlib
import numpy as np
import matplotlib.backends.backend_tkagg
import matplotlib.pyplot as plt
import time

#pour convertir en exe: pyinstaller -F nom_fichier.py

#########
#Fonction euler :
#=================
def eulersolver(posi_init,temps_init,temps_fin,dx_dt):
    x0=float(posi_init);
    t0=float(temps_init);
    tmax=float(temps_fin);
    fun=str(dx_dt);

    h=0.01;     #pas d'integration
    N=int(abs(tmax-t0)/h)+1; #+1 pour arriver a T[N]=Tmax

    X=np.zeros(N) #indice de n=0 -> N-1 (il y en a N)
    T=np.zeros(N)
    def F(x1,t1):
        x=x1;
        t=t1;
        F=eval(fun);
        return F;
    T[0]=t0;
    X[0]=x0;
    data=open("data.txt","w") #cré le fichier de datas
    data.write("t\t;x\n") #y met les titres
    data.write(str(T[0])+"\t;"+str(X[0])+"\n") #les premiers points
    for i in range(1,N):
        T[i]=T[i-1]+h;
        X[i]=X[i-1]+F(X[i-1],T[i-1])*h;
        data.write(str(T[i])+"\t;"+str(X[i])+"\n") #enregistre les points dans data

    data.close();#ferme data
    plt.plot(T,X,label="Solution");
    plt.title('EulerSolver représentation');
    plt.xlabel('t');
    plt.ylabel('x(t)');
    axes=plt.gca()
    axes.set_xlim([t0,tmax])
    plt.show();
    legend()

    if askyesno('Fin de résolution', 'Encore une résolution ?'):
        showwarning('Attention', 'Le présent fichier data.txt sera effacé !')
    else:
        showinfo('Merci', "Merci d'avoir utilisé mon application !")
        showerror("Pense-bête", "Fichier data.txt créé !")
        app.destroy();
    return

####################################### FIN FONCTION EULER
app=Tk();
app.title("EulerSolver")
pre="""
Programme permettant de résoudre une ODE d'ordre 1
avec la méthode dite d'Euler. EDO du type dx(t)/dt=F(x,t).
Pensez à écrire avec la convention python (pas de multiplication
implicite, pas pas de '^' pour les puissance, et pensez à mettre
des virgules aux chiffres (5.0 au lieu de 5 tout court).
"""
presentation=Label(app,text=pre,padx=25,pady=10)
presentation.pack()

#Domaine
DOM=LabelFrame(app,text="Limites du calcul", padx=25, pady=20)
DOM.pack(fill="both", expand="yes")
Label(DOM,text="Domaine temporel de résolution :").pack(side=LEFT)

temps_init=StringVar() 
temps_init.set("temps initial")
TI=Entry(DOM, textvariable=temps_init, width=15,justify=LEFT)
TI.pack()

saut=Label(DOM,text='',pady=2).pack()

temps_fin=StringVar() 
temps_fin.set("temps final")
TF=Entry(DOM, textvariable=temps_fin, width=15,justify=LEFT)
TF.pack()

#CI
CI=LabelFrame(app,text="Conditions initiales", padx=25, pady=15)
CI.pack(fill="both", expand="yes")
Label(CI,text="Donner la valeur initiale de x :").pack(side=LEFT)
cond_init=StringVar() 
cond_init.set("x(t=t0)")
X0=Entry(CI, textvariable=cond_init, width=15)
X0.pack()

#Fonction
FUNC=LabelFrame(app,text="Fonction", padx=25, pady=15)
FUNC.pack(fill="both", expand="yes")
Label(FUNC,text="Donnez dx/dt en terme de x et t seulement :").pack(side=LEFT)
derivee=StringVar() 
derivee.set("dx/dt")
F=Entry(FUNC, textvariable=derivee, width=15)
F.pack()

message=Label(app,text="Si tout es rempli, on peut résoudre !",padx=25,pady=5)
message.pack()

resolution=Button(app, text="Résoudre", command=lambda : eulersolver(X0.get(),TI.get(),TF.get(),F.get()))
resolution.pack()

fin=Label(app,text="(c)*********",padx=25,pady=5)
fin.pack()
app.mainloop();
John
  • 303
  • 4
  • 13
  • did you read `pyinstaller` documentation ? Did you try [Listing Hidden Imports](http://pythonhosted.org/PyInstaller/when-things-go-wrong.html#listing-hidden-imports) – furas Dec 22 '16 at 17:20
  • First : no it is note the same question, it is actually not the same code at all. And when I run the line `pyinstaller -v app.py` it returns 32 ? I don't understand ! – John Dec 22 '16 at 17:53
  • Can you please help me understanding the problem ? – John Dec 22 '16 at 18:00
  • I run Linux and I don't get error `No module named 'tkinter'` but I get error `No module named 'tkinter.filedialog'` and I had to add in code `import tkinter.filedialog` or run `pyinstaller --hidden-import=tkinter.filedialog -F app.py` BTW `pyinstaller` creates file `app.spec` with configuration and you can set `hidden-import` inside this file. – furas Dec 22 '16 at 21:04
  • BTW: maybe it is not the same code but you get the same error so it is duplicate question. – furas Dec 22 '16 at 21:06
  • `pyinstaller -v` returns version of `pyinstaller`. Try `pyinstaller --help` to see description – furas Dec 22 '16 at 21:07

0 Answers0