我正在尝试使用 pyinstaller 将我的项目打包成可执行文件。 main.py,我的程序,应该在我运行 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 个文件,build
和dist
正如预期的那样。
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
但运行时出现同样的错误。
最佳答案
tkinterdnd2
website 中有关于 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=.
关于python - (Pyinstaller) 'tclError: can' t find package tkdnd' 如何修复此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68955373/