【发布时间】:2019-08-13 03:59:14
【问题描述】:
背景
我正在关注this link 进行人脸识别,我想使用 Python 构建一个独立的应用程序。我的main.py 脚本如下所示。
# main.py
# Import packages and other scripts
import tkinter as tk
...
# Some functions
def initialization():
# OpenCV and sklearn are used here
...
def registration():
# OpenCV, and sklearn are used here
...
def face_recognition():
# OpenCV and sklearn are used here
...
# Start the Tkinter GUI
window = tk.Tk()
# Initialize the face recognition model
initialization()
# Input name for registration
tk.Label(window, text = "Name").grid(...)
entry1 = tk.Entry(window)
entry1.grid(row=0, column=1)
# When the button is clicked, different command is executed
tk.Button(window, text='Registration', command=registeration).grid(...)
tk.Button(window, text='Face Recognition', command=face_recognition).grid(...)
window.mainloop()
使用python解释运行脚本(python main.py),一切正常。
问题
我使用 Pyinstaller 通过以下命令将脚本转换为单个 exe:
pyinstaller --onefile \
--windowed \
--hidden-import sklearn.neighbors.typedefs \
main.py
然后,我生成了两个 exe。第一个在dist/main,第二个在dist/main.app/Contents/MacOS/main
运行exe时,exe会不断重复。我显示正在运行的进程,发现了这个结果
$ ps
PID TTY TIME CMD
... ... ... /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(8)
... ... ... /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(8)
... ... ... /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(7)
... ... ... /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(7)
... ... ... /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(8)
我不知道 exe 会发生什么,因为我没有在我的脚本中导入多处理包。知道如何解决这个问题吗?谢谢
更新 1
我在使用Pyinstaller 时添加了--log-level ERROR 并给出了这个结果。不确定是否与我的问题有关。
Traceback (most recent call last):
File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'Crypto.Math'
174598 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
176282 ERROR: Can not find path ./libtbb.dylib (needed by /Users/user/anaconda3/lib/libmkl_tbb_thread.dylib)
更新 2
我发现我正在使用的软件包之一 -- imultis VideoStream 涉及threading。我想这是观察from multiprocessing.semaphore_tracker 的原因,即使我没有明确import multiprocessing。
然后遇到this post,建议加multiprocessing.freeze_support()。它可以抑制 GUI 窗口不断重复,但使用 $ ps 显示的后台进程仍然保持重复。问题还没有解决。
更新 3(问题已找到但未修复)
调试了半天代码,发现这个死循环的原因不是imultisVideoStream的threading造成的(我写了另一个脚本测试imultis完全OK )。但是问题来自于导入sklearn!!!此问题已在 GitHub 中this link 报告。
此 GitHub 链接还建议包含 multiprocessing.freeze_support()。此外,其中一个响应建议在调用multiprocessing.freeze_support() 之后导入sklearn。我尝试了一个简单的脚本,但问题仍然存在。
结论:sklearn 导致可执行文件不断打开。但我不知道为什么会这样,也不知道如何解决。任何帮助,将不胜感激。谢谢。
【问题讨论】:
-
您可能需要在 main.py 中使用 waitkey 来等待某些键破坏您的窗口。并且还使用 destroyAllWindows。
标签: python pyinstaller