【问题标题】:(Pyinstaller) 'tclError: can't find package tkdnd' How can I fix this error?(Pyinstaller) 'tclError: can't find package tkdnd' 我该如何解决这个错误?
【发布时间】:2021-10-27 12:13:30
【问题描述】:

我正在尝试使用 pyinstaller 将我的项目打包成可执行文件。我的程序 main.py 应该在我运行 exe 文件时运行。 Exe,因为程序必须以某种方式运行,而无需用户(无论如何都不是开发人员)需要安装模块和 python 本身。如果有更好/更简单的方法,请告诉我。

我使用 pip install tkinterdnd2 安装了 tkinterdnd2。

不确定这是否有必要,但这是程序的目录:(png仅用于我的程序,我不认为这是罪魁祸首):

Folder
- cardiologist.png
- img.png
    .
    . # a couple more pngs
    .
- main.py
- patients' data csv.csv
- patients' data xlsx test.xlsx
- sample.xlsx
- sun-valley.tcl #this file and the 'theme' folder below are for my program's theme, also don't think these are the culprit
- theme
    - dark
        - a lot of pngs...
    - dark.tcl
    - light
        - a lot of pngs...
    - light.tcl

Pyinstaller 按预期创建了 2 个文件,builddist

build
    - main
        - base_library.zip
        - certifi
        - IPython
        - jedi
        - (some other files)
                .
                . a lot of .dylib files
                .
        - main    # exe file to execute to run program
        - matplotlib (this and next 4 are folders)
        - numpy
        - pandas
        - parso
        - PIL
        - Python  # exe file, don't know what this was created for
        - (some more folders)
        - tcl (this and next 2 are folders)
        - tcl8
            - 8.4
                - platform
                    - .tm file
                - .tm file
            - 8.5
                - 2 .tm files
            - 8.6
                - .tm file
                - tdbc
                    - .tm file
                - tkdnd2.8 (tried renaming to just tkdnd but same error)
        - tk
        - ttkwidgets
        - 2 other folders

我使用的命令: python -m PyInstaller main.py. # without python -m, 'command not found error' would happen

运行生成的exe时出错:

Traceback (most recent call last):
  File "tkinterdnd2/TkinterDnD.py", line 53, in _require
_tkinter.TclError: can't find package tkdnd

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 16, in <module>
  File "tkinterdnd2/TkinterDnD.py", line 285, in __init__
  File "tkinterdnd2/TkinterDnD.py", line 55, in _require
RuntimeError: Unable to load tkdnd library.
[7015] Failed to execute script 'main' due to unhandled exception!
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

我已经上网并尝试寻找答案,但它们要么对我不起作用,要么像这样不够清楚:*.py script with tkinterdnd2 module doesn`t compile to an executable file(这个解决方案仍然给了我同样的错误)。

我也尝试过使用其他模块进行打包,例如 cx_freeze 和 py2app,但都产生了不同的错误,所以我现在回到 pyinstaller。我在 macOS 上。

有什么办法可以解决这个错误?

编辑

也尝试了python -m PyInstaller --clean -y -n "output_name" --add-data="tkdnd:tkdnd" main.py,但运行时产生了同样的错误。

【问题讨论】:

  • 尝试直接从github上使用库github.com/petasis/tkdnd/tree/master/library
  • 既然您安装了tkinterdnd2,您是否在配置文件中尝试过- tkinterdnd2
  • @PetrL。刚刚尝试从那里下载整个 zip,将整个文件夹放入 tcl 8.6,甚至将其重命名为 tkdnd,仍然会弹出相同的错误。将它放在与 main 相同的目录中也会产生相同的错误。
  • @BryanOakley 我可以知道你在说什么配置文件吗?

标签: python tkinter pyinstaller exe


【解决方案1】:

我不确定其他答案的第一手经验,但我个人在我的应用程序中分发 tkdnd。当然,在 tkinterdnd2 在 PyPi 中可用之前,我首先这样做了。我使用了 tkdnd 发行版和 python 包装器。 在我的构建脚本中,我有

copy_tree('build_files/tkdnd2.9.2', os.path.dirname(sys.executable) + '/tcl/tkdnd2.9.2')
copy_tree('build_files/TkinterDnD2', os.path.dirname(sys.executable) + '/Lib/site-packages/TkinterDnD2')

在我的规范文件中,

tkdnd = [(os.path.abspath(file), 'tkdnd2.9.2') for file in iglob('build_files/tkdnd2.9.2/*.*')]
data_files = [...] + tkdnd
a = Analysis9(..., datas=data_files,...)

您可以进一步查看我的项目,Music Caster

【讨论】:

    【解决方案2】:

    tkinterdnd2website中有一个关于PyInstaller的部分。

    基本上需要的步骤是:

    • hook-tkinterdnd2.py 从网站复制到您的项目文件夹或创建包含以下内容的文件:
    from PyInstaller.utils.hooks import collect_data_files
    
    datas = collect_data_files('tkinterdnd2')
    
    • 运行PyInstaller如下:
    pyinstaller -F main.py --additional-hooks-dir=.
    

    或根据您问题中的命令:

    python -m PyInstaller -F main.py --additional-hooks-dir=.
    

    【讨论】:

      【解决方案3】:

      我终于不用手动移动文件夹就让它工作了。

      pip install tkinterdnd2
      

      我使用 auto py 来执行,但它不适用于“--add-binary”,所以我在 gui 中使用了“add-folder”,它基本上只是“--add-data”。

      pyinstaller --noconfirm --onefile --windowed --add-data "<python_path>/Lib/site-packages/tkinterdnd2;tkinterdnd2/" "<your_script>"
      

      【讨论】:

        【解决方案4】:

        这里的问题是 Drag n Drop 需要两个组件:TCL 库和它们的 Tkinter 接口。我手动安装两者来配置我的环境。 (请参阅我对How to Install and Use TkDnD with Python Tkinter on OSX? 的回答)。我知道有人在 PyPI 上为 TkinterDnD2 打包了一些东西,但我还没有调查过。

        我有一个使用 TkinterDnD2 的项目。我使用 PyInstaller 构建它并看到您看到的相同错误(或多或少)。使用 --onedir 选项(而不是 --onefile 选项)运行 PyInstaller,我发现我的 dist 目录中缺少 tkdnd2.8

        为了解决这个问题,我在 Windows 上添加了

        --add-binary "C:/Python/Python38-32/tcl/tkdnd2.8;tkdnd2.8"
        

        到 PyInstaller 命令行,就成功了。我现在可以构建一个 --onefile 可执行文件,它可以正常运行。

        您尝试了类似的方法,但我使用了 --add-binary 而不是 --add-data,并且我提供了库的完整路径。

        【讨论】:

          猜你喜欢
          • 2016-07-21
          • 2016-09-09
          • 2015-02-28
          • 2013-08-16
          • 2020-04-17
          • 1970-01-01
          相关资源
          最近更新 更多