python - "Failed to execute script "pyinstaller

标签 python pyinstaller

我有一个 python 代码,我想将其转换为 exe。
我使用 pyinstaller : pyinstaller --onefile -w script.py
完成生成 exe 文件后,我双击该文件,但出现“无法执行脚本”。

我也尝试从 cmd 运行它,但它给出了相同的 fatal error 。

要添加的内容:
该代码导入了几个文件包,包括我制作的 python 代码,并且它使文件引用了它的位置。

我是不是做错了什么?

该脚本具有这些导入:

import socket
import os
from PIL import ImageGrab
import cv2
import time
import json
import myFile

我在cmd下运行代码,它给出了这个错误:

  File "script.py", line 3, in <module>
    from PIL import ImageGrab
ModuleNotFoundError: No module named 'PIL'

可能不相关,但现在我尝试执行pyinstaller --onefile -w client.py
我运行后,Windows Defender 在里面发现了这个:
enter image description here


在 pycharm 的终端中运行它后:

pyinstaller --onefile --hidden-import=PIL --hidden-import=Pillow --hidden-import=pynput client.py

我收到此错误(请注意,我将其从 dist 目录移至主目录):

Traceback (most recent call last):
  File "client.py", line 7, in <module>
    import myFile
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "myFile.py", line 1, in <module>
    from pynput import mouse, keyboard
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "pynput\__init__.py", line 40, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "pynput\keyboard\__init__.py", line 31, in <module>
  File "pynput\_util\__init__.py", line 82, in backend
ImportError
[13364] Failed to execute script client

最佳答案

这可能是由于 pyinstaller 无法正确找到您的依赖项并跳过一些软件包。

要修复任何错误,例如 ModuleNotFoundError: No module named 'PIL',只需将其添加为隐藏导入即可:

pyinstaller --onefile --hidden-import=PIL -w script.py

对于第二个错误,这是 pyinstaller 和 pynput 的已知问题。 查找here一些解释。

TLDR 似乎是您需要添加 --hidden-import=pynput.mouse._win32 --hidden-import=pynput.keyboard._win32 --hidden-import=pynput._util。 _win32 以及任何其他给您带来错误的子包。

关于python - "Failed to execute script "pyinstaller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67489963/

相关文章:

python - pyinstaller 应用程序的日志记录工作错误 : calling module and function are set to 'logging' and 'debug'

python - OpenGL - 一些相交和一些非相交多边形的曲面分割

python - 在一个 AWS Redshift 实例下创建(虚拟)多个数据库

Python 获取 future (x) 天和剩余小时数的日期?

python - 应用程序包中的 PyInstaller Tkinter 窗口分辨率低,但应用程序中没有

python - 仅通过结合 PyInstaller 导入有用的函数可以将文件大小减少多少?

python - pyinstaller exe不会关闭

python - PyInstaller 和 Enthought Suite

python - 通过 gensim 将新词添加到 GoogleNews

python - wxPython - 如何对 ListCtrl 列项目进行排序?