FileNotFound在使用 pyinstaller 编译的 .exe 文件中导入 Scipy 时出错

FileNotFoundError when importing Scipy in a .exe file compiled with pyinstaller

提问人:P.Bryss 提问时间:8/1/2023 最后编辑:BDLP.Bryss 更新时间:8/3/2023 访问量:43

问:

我正在尝试使用 pyinstaller 运行编译成 .exe 的 python 脚本,并且在导入 scipy 时遇到问题(其他导入的包没有问题):

Traceback (most recent call last):
  File "Sim_choix_modal.py", line 15 in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\yimode03_importers.py", line 495, in exec_module
  File "scipy\__init__.py, line 76 in <module>
  File "scipy\__init__.py, line 67 in _delvewheel_init_patch_1_1_0
  File "os.py", line 1111, in add_dll_directory
FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable: 'C:\\Users\\username~1\\AppData\\Local\\Temp\\_MEI155082\\scipy.libs'
[17952] Failed to execute script 'Sim_choix_modal' due to unhandled exception!

一些情报:

  • python 脚本名为“Sim_choix_modal.py”
  • Sim_choix_modal.py中的第 15 行是import scipy
  • [WinError 2] 后面的法语文本等同于“No such file or directory: 'C:\Users\...”。
  • 为了编译我的 python 脚本,我使用以下命令pyinstaller --onefile Sim_choix_modal.py

我读过类似的问题,并尝试过似乎对其他用户有用的东西:

  1. 在将 .py 编译为 .exe 时添加该选项,使 .exe 在运行时关闭(崩溃?),而不会显示任何错误 (来源:PyInstaller FileNotFoundError “scipy.libs”--add-data "path/to/venv/Lib/site-packages/scipy.libs;scipy.libs/" )

  2. 将 hiddenimports=['scipy._lib.messagestream'] 添加到规范文件中(在将.py编译成 .exe 之前和之后尝试过:似乎没有改变任何东西,相同的错误消息。 (来源:使用 pyinstaller 后执行编译文件“没有名为 scipy._lib.messagestream' 的模块”时出错)

  3. 遵循此回复,无济于事:https://stackoverflow.com/a/62238104/22319153(如果我理解正确的话,应该在pyinstaller中添加一个scipy的钩子)

提前感谢您抽出宝贵时间,

保罗

python scipy pyinstaller filenotfounderror

评论

0赞 BDL 8/2/2023
请不要将答案编辑到问题中

答:

0赞 P.Bryss 8/3/2023 #1

我设法让它工作,尽管我不太确定是什么诀窍。

  1. 我更新了 scipy
  2. 基于这篇文章(https://stackoverflow.com/a/47979807/22319153)我在编译时删除了 --onefile 选项,然后将 scipy 目录(安装它的位置)的内容复制到另一个 scipy 目录(在可执行目录中)
  3. 根据这篇文章(https://stackoverflow.com/a/47979807/22319153),在 .spec 文件中,我在 = Analysis 中填写了 Data = [...] (将“plotly”替换为“scipy”,并在“scipy.libs”的数据中添加另一个元素