2016-02-21 13 views
14

Quando uso cx_Freeze Ottengo un keyerror KeyError: 'TCL_Library' mentre costruisco il mio programma pygame. Perché ottengo questo e come lo risolvo?KeyError: 'TCL_Library' quando uso cx_Freeze

mio setup.py è qui sotto:

from cx_Freeze import setup, Executable 

setup(
    name = "Snakes and Ladders", 
    version = "0.9", 
    author = "Adam", 
    author_email = "Omitted", 
    options = {"build_exe": {"packages":["pygame"], 
         "include_files": ["main.py", "squares.py", 
         "pictures/Base Dice.png", "pictures/Dice 1.png", 
         "pictures/Dice 2.png", "pictures/Dice 3.png", 
         "pictures/Dice 4.png", "pictures/Dice 5.png", 
         "pictures/Dice 6.png"]}}, 
    executables = [Executable("run.py")], 
    ) 

risposta

26

È possibile risolvere questo errore impostando manualmente le variabili di ambiente:

set TCL_LIBRARY=C:\Program Files\Python35-32\tcl\tcl8.6 
set TK_LIBRARY=C:\Program Files\Python35-32\tcl\tk8.6 

È anche possibile farlo nello script setup.py:

os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tcl8.6' 
os.environ['TK_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tk8.6' 

setup([..]) 

Ma ho scoperto che in realtà il programma non funziona. Sul cx_freeze mailinglist it was mentioned:

I have looked into it already and no, it is not just a simple recompile -- or it would have been done already! :-)

It is in progress and it looks like it will take a bit of effort. Some of the code in place to handle things like extension modules inside packages is falling over -- and that may be better solved by dropping that code and forcing the package outside the zip file (another pull request that needs to be absorbed). I should have some time next week and the week following to look into this further. So all things working out well I should put out a new version of cx_Freeze before the end of the year.

Ma forse hai più fortuna ... Here's the bug report.

+0

Dove si imposta le variabili d'ambiente manualmente? – Orange1861

+0

@ Orange1861 Nel prompt di cmd.exe; con il comando nel post ... – Carpetsmoker

+0

Ci scusiamo per l'attesa. Grazie per l'aiuto, non ho potuto farlo funzionare. – Orange1861

10

Basta mettere questo prima l'installazione in setup.py

import os 

os.environ['TCL_LIBRARY'] = "C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6" 
os.environ['TK_LIBRARY'] = "C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tk8.6" 

ed eseguirlo:

python setup.py bdist_msi 

questo ha funzionato bene per me.

4

Se l'errore con Python 3.6 si ottiene seguente:

copying C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6 -> build\exe.win-amd64-3.6\tcl 
error: [Errno 2] No such file or directory: 'C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6' 

sufficiente creare LOCAL_TO_PYTHON dir in C:\ quindi creare Python35-32 dir suo interno. Ora copia tcl dir dalla directory Python36 esistente (in C:\) in Python35-32.

Quindi funziona correttamente.

17

Invece di impostare le variabili d'ambiente utilizzando installazione percorsi assoluti specifici come C:\\LOCAL_TO_PYTHON\\... si può anche ricavare i percorsi necessari utilizzando in modo dinamico l'attributo __file__ del pacchetto standard di Python come os:

import os.path 
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__)) 
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') 

Dopo questa correggere il file eseguibile essere creato, ma probabilmente si otterrà un "errore DLL non trovato" quando si tenta di eseguirlo - almeno con Python 3.5.3 e cx_Freeze 5.0.1 su Windows 10.

Quando si aggiungono le seguenti opzioni, file DLL necessario s vengono automaticamente copiati dalla directory Python-installazione per l'accumulo di uscita del cx-Freeze e si dovrebbe essere in grado di eseguire l'applicazione Tcl/Tk:

options = { 
    'build_exe': { 
     'include_files':[ 
      os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), 
      os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), 
     ], 
    }, 
} 

# ... 

setup(options = options, 
     # ... 
) 
+0

Wow, questo ha funzionato perfettamente! Non ho nemmeno ricevuto un errore 'DLL non trovato ', quindi non è necessario aggiungere opzioni extra come specificato nella seconda parte della risposta. Ho Python 3.6 e la mia py-app usa Tkinter/TCL. – Alex

0

Se l'errore con Python 3.6 si ottiene seguente:

copia C: \ LOCAL_TO_PYTHON \ Python35-32 \ tcl \ tcl8.6 -> build \ exe.win-amd64-3.6 \ tcl errore: [Errno 2] Nessun file o directory di questo tipo: 'C: \ LOCAL_TO_PYTHON \ Python35-32 \ tcl \ tcl8.6 ' Basta creare la dir LOCAL_TO_PYTHON in C: \, quindi creare la dir Python35-32 al suo interno.Ora copia tcl dir dalla directory Python36 esistente (in C :) in Python35-32.

Quindi funziona correttamente.

** Ho fatto questa procedura e creato un file exe nella directory di compilazione, ma se provo a cliccare app Non aspettare sullo schermo immediatamente veloce, i miei codici qui **

from tkinter import * 
import socket 



window=Tk() 
window.geometry("400x150") 
window.title("IpConfiger") 
window.config(background="black") 

def goster(): 
    x=socket.gethostbyname(socket.gethostname()) 
    label=Label(window,text=x,fg="green",font=("Helvetica",16)) 
    label.pack() 
def information(): 
    info=Label(window,text="Bu program anlık ip değerini 
    bastırır.",fg="green",font=("Helvetica",16),bg="black") 
    info.pack() 


information() 
tikla=Button(window,text="ip göster",command=goster) 

tikla.pack()