我有一个基本的 python 脚本,它是一个语音识别脚本,可以让我通过语音命令关闭我的电脑,还有一些额外的东西可以有一个托盘图标。我已经完全提供了所有代码,因为它并不多。当我使用 python 通过命令行运行它时,这非常有效。但是,当我使用 pyinstaller 将其打包到 exe 文件中时,出现以下错误:
我基本上使用每个可用的参数运行 pyinstaller,但据我了解,这些应该是我需要的参数:
pyinstaller --noconsole --onefile --add-data icon.png;. filename.py
我还尝试使用控制台作为目录而不是单个文件等。总是得到相同的错误。
# Imports
import sys
import pystray
import os
import speech_recognition as sr
from PIL import Image
from pystray import Menu, MenuItem
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
# Kill
def exit_action(icon):
sys.exit(0)
# Tray icon
icon = pystray.Icon('pc-off',)
icon.menu = Menu(MenuItem('Exit', lambda : exit_action(icon)),)
icon.icon = Image.open(resource_path('icon.png'))
icon.title = 'pc-off'
icon.run()
# Voice Recognition
def callback(recognizer, audio):
try:
if(recognizer.recognize(audio) == "Python shut down PC" or recognizer.recognize(audio) == "Python shutdown PC" ):
os.system("shutdown /s /t 1")
except LookupError:
pass
r = sr.Recognizer()
r.listen_in_background(sr.Microphone(), callback)
import time
while True: time.sleep(0.1)