You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包Pygame游戏时提示文件含病毒的解决方法

问题描述

我用Pygame开发了一款名为《Balls to the Walls》的游戏,使用PyInstaller打包时执行以下命令:

C:\Users\Ben pyinstaller --windowed --icon=logo.ico  BallsWalls.py

遇到如下报错:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "B:\Python Installs\Python 3.11.6\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\__main__.py", line 205, in _console_script_run
    run()
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\__main__.py", line 189, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\__main__.py", line 61, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\building\build_main.py", line 1033, in main
    build(specfile, distpath, workpath, clean_build)
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\building\build_main.py", line 973, in build
    exec(code, spec_namespace)
  File "C:\Users\Student\Balls to the Walls\BallsWalls.spec", line 18, in <module>
    exe = EXE(
          ^^^^
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\building\api.py", line 625, in __init__
    self.__postinit__()
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
    self.assemble()
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\building\api.py", line 738, in assemble
    self._retry_operation(icon.CopyIcons, build_name, self.icon)
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\building\api.py", line 992, in _retry_operation
    return func(*args)
           ^^^^^^^^^^^
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\utils\win32\icon.py", line 209, in CopyIcons
    return CopyIcons_FromIco(dstpath, [srcpath])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\PyInstaller\utils\win32\icon.py", line 143, in CopyIcons_FromIco
    hdst = win32api.BeginUpdateResource(dstpath, 0)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\win32ctypes\pywin32\win32api.py", line 208, in BeginUpdateResource
    with _pywin32error():
  File "B:\Python Installs\Python 3.11.6\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "B:\Python Installs\Python 3.11.6\Lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
    raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (225, 'BeginUpdateResourceW', 'Operation did not complete successfully because the file contains a virus or potentially unwanted software.')

尝试过其他方案无效,求可行修复方法。


解决方案

这个错误是Windows Defender(或其他杀毒软件)把PyInstaller生成的临时文件/最终exe误判为恶意软件导致的,试试以下几种方法:

  • 临时关闭实时保护打包:
    打开Windows设置→隐私和安全性→Windows安全中心→病毒和威胁防护→管理设置,暂时关闭“实时保护”,完成打包后立刻重新开启。这是最快的临时解决办法,但注意打包期间不要浏览不安全网站。

  • 添加目录到杀毒软件排除项:
    同样在病毒和威胁防护设置里,找到“添加或删除排除项”,把PyInstaller生成的build、dist文件夹,以及你的游戏项目根目录都添加进去。这样后续打包时杀毒软件就不会扫描这些目录,避免误拦截。

  • 改用目录模式打包:
    PyInstaller默认的单文件(--onefile)模式生成的exe容易被误报,换成目录模式(--onedir)打包,命令改成:

    pyinstaller --windowed --icon=logo.ico --onedir BallsWalls.py
    

    这种模式会生成一个包含所有依赖的文件夹,分发时需要把整个文件夹交给用户,但被杀毒软件误判的概率低很多。

  • 提交误报申诉:
    如果打包成功后杀毒软件仍然报毒,你可以向杀毒软件厂商提交误报申诉,上传你的exe文件并说明这是你自己开发的游戏软件,审核通过后就不会再被拦截了。


内容的提问来源于stack exchange,提问作者Benjamin Ecker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:43:10