PyInstaller + tkdnd/tkinterdnd2 "Unable to load tkdnd library" when launching frozen exe. Works when launched from script
Asked Answered
P

3

6

In my script I am using tkinterdnd2 library to achieve drag and drop functionality from Windows explorer into my tkinter UI.

from tkinterdnd2 import TkinterDnD, DND_FILES
import tkinter as tk

class TkWindow:
    def __init__(self):
        self.window = TkinterDnD.Tk()
        self.tbox = tk.Listbox(self.window)
        self.tbox.pack(fill=tk.BOTH)
        self.tbox.drop_target_register(DND_FILES)
        self.tbox.dnd_bind('<<Drop>>', self.tk_files_dropped)
        self.window.mainloop()

    def tk_files_dropped(self, event):
        messagebox.showinfo("x", event.data)

TkWindow()

When I launch the script - everything works.

But when I freeze the project to a single EXE with PyInstaller, and run it, I get this error:

Unable to load tkdnd library.

I tried this solutions already:

  1. I added the pyinstaller-hook as instructed in the tkinterdnd2 repository:

    from PyInstaller.utils.hooks import collect_data_files, eval_statement
    datas = collect_data_files('tkinterdnd2')

  2. I add --collect-all tkinterdnd2 when executing build command.

  3. I tried copying tkdnd2.8 to tcl8.6 as mentioned in this answer

  4. I tried getting rid of venv and installing all the packages directly into base python interpreter.

Prostatitis answered 27/9, 2021 at 14:39 Comment(5)
when I was having issues with pyinstaller, I tried different methods which didn't work until I run pip uninstall typing in command prompt and it worked fine.Granoff
@CEO do you mean uninstalling and installing pyinstaller? Tried that too.Prostatitis
not really. Let me write it in the answer section thenGranoff
Does your code run without any error in the code editor?Granoff
Yes. Without any error. Full repo is here: github.com/deshudiosh/PyWykladzinyLayoutProstatitis
A
3

I used --collect-all TkinterDnD2 with upperCase.

Appellate answered 4/12, 2021 at 23:39 Comment(2)
I already switched to wxpython but I am glad you answered. Seams that this might be the reason.Prostatitis
I had to make it lower case. The full command I used was, python -m PyInstaller --noconsole -F --collect-all TkinterDnD2 main.pyJoshia
G
0

To convert a Python file to an exe, run pyinstaller.exe --collect-all TkinterDnD2 --windowed yor_app.py. This will create files and folders of the program that was created. You will find a file named TCL. Copy the tkdnd folder into it and then run the exe file

Graeme answered 18/5, 2022 at 20:11 Comment(2)
pyinstaller.exe --collect-all TkinterDnD2 --windowed yor_app.pyGraeme
Doesn't the other answer already cover this? In any case, please mark code with backticks (`), not parentheses. See stackoverflow.com/help/formattingThereabout
H
0

None of the other answers worked for me.

I was successfull with:

pyinstaller -F --additional-hooks-dir=. --windowed gui.py

Plus using the hook file from the git repository https://github.com/pmgagne/tkinterdnd2/blob/master/hook-tkinterdnd2.py (MIT license):

"""pyinstaller hook file.

You need to use this hook-file if you are packaging a project using tkinterdnd2.
Just put hook-tkinterdnd2.py in the same directory where you call pyinstaller and type:

    pyinstaller myproject/myproject.py --additional-hooks-dir=.
"""

from PyInstaller.utils.hooks import collect_data_files, eval_statement


datas = collect_data_files('tkinterdnd2')

(There's a hint in https://pypi.org/project/tkinterdnd2/ at the bottom of the page.)

Hystero answered 5/8 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.