簡體   English   中英

ModuleNotFoundError:沒有名為 'pyttsx3.drivers' 的模塊(使用 pyinstaller 編譯的文件),但未編譯時工作正常

[英]ModuleNotFoundError: No module named 'pyttsx3.drivers' (File Compiled with pyinstaller), but working fine as uncompiled

我使用 pyinstaller 編譯了我的程序,python 文件在未編譯時工作正常,但在我編譯和測試時拋出錯誤。

這是完整的錯誤,我認為這可能是因為 pyinstaller

Traceback (most recent call last):
  File "site-packages\pyttsx3\__init__.py", line 20, in init
  File "c:\python37\lib\weakref.py", line 137, in __getitem__
    o = self.data[key]()
KeyError: 'sapi5'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "song_dl.py", line 25, in <module>
    engine = pyttsx3.init('sapi5')
  File "site-packages\pyttsx3\__init__.py", line 22, in init
  File "site-packages\pyttsx3\engine.py", line 30, in __init__
  File "site-packages\pyttsx3\driver.py", line 50, in __init__
  File "importlib\__init__.py", line 127, in import_module
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'pyttsx3.drivers'
[1072] Failed to execute script song_dl

首先,go 到你的 python.exe 文件安裝的文件夾,然后 go 到這個目錄:

\Lib\site-packages\PyInstaller\hooks

在進入目錄之前,您必須先安裝 pyinstaller。 然后查看 hooks 文件夾,看看是否有一個文件名為:

鈎-pyttsx3.py

該文件很可能不存在。 因此,您必須在 hooks 文件夾和您必須編寫的文件中創建hook-pyttsx3.py

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2020, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


""" pyttsx3 imports drivers module based on specific platform. Fount at https://github.com/nateshmbhat/pyttsx3/issues/6 """


hiddenimports = [
    'pyttsx3.drivers',
    'pyttsx3.drivers.dummy',
    'pyttsx3.drivers.espeak',
    'pyttsx3.drivers.nsss',
    'pyttsx3.drivers.sapi5', ]

保存文件。 然后運行你的代碼。 問題將得到解決(至少它對我有用。)這個問題的出現是因為 pyinstaller 沒有正式更新為完全使用 python 3.8,所以缺少一些模塊的鈎子

查看文檔的When things go wrong部分,特別是, Listing hidden imports

看起來 pyinstaller 無法“知道”它需要添加此特定模塊,因此您需要明確指定它

可能是這樣的

$ pyinstaller --hidden-import=pyttsx3.drivers song_dl.py

@Lakshya 給出的答案是正確的,但僅限於 Windows 環境。

對於使用 Anaconda 的用戶,路徑會發生變化。 為了新用戶的簡單性,這里是更新的路徑。

在這里,我使用了一個名為voice的虛擬環境,因此它與python3.7一起位於envs/voice/中。 這些可能會根據您的本地路徑而有所不同。

/anaconda3/envs/voice/lib/python3.7/site-packages/PyInstaller/hooks

如果你沒有使用虛擬環境,它看起來像,

/anaconda3/lib/python3.8/site-packages/PyInstaller/hooks

然后只需添加文件hook-pyttsx3.py

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2020, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


""" pyttsx3 imports drivers module based on specific platform. Fount at https://github.com/nateshmbhat/pyttsx3/issues/6 """


hiddenimports = [
    'pyttsx3.drivers',
    'pyttsx3.drivers.dummy',
    'pyttsx3.drivers.espeak',
    'pyttsx3.drivers.nsss',
    'pyttsx3.drivers.sapi5', ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM