在 pyglet 项目中导入 numba 模块时出错

error while import numba module in pyglet projects

请问为什么我在pyglet项目中导入numba模块,加载图片或声音文件时会报错

import pyglet
import numba

game_window = pyglet.window.Window(600, 400)
icon = pyglet.image.load("ship.png")
game_window.set_icon(icon)
pyglet.app.run()

我收到以下错误:

C:\Users\BOB\Desktop> cd c:\Users\BOB\Desktop && cmd /C "C:\Users\BOB\Documents\Python38\python.exe c:\Users\BOB\.vscode\extensions\ms-python.python-2020.5.86806\pythonFiles\lib\python\debugpy\no_wheels\debugpy\launcher 49521 -- c:\Users\BOB\Desktop\p.py "
Traceback (most recent call last):
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\__init__.py", line 334, in __getattr__
    return getattr(self._module, name)
AttributeError: 'NoneType' object has no attribute 'load'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\image\codecs\wic.py", line 290, in __init__
    ole32.CoInitializeEx(None, COINIT_MULTITHREADED)
  File "_ctypes/callproc.c", line 948, in GetResult
OSError: [WinError -2147417850] Изменение режима для потока после его установки невозможно

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\BOB\Documents\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\BOB\Documents\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\BOB\.vscode\extensions\ms-python.python-2020.5.86806\pythonFiles\lib\python\debugpy\no_wheels\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\BOB\.vscode\extensions\ms-python.python-2020.5.86806\pythonFiles\lib\python\debugpy\no_wheels\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\BOB\.vscode\extensions\ms-python.python-2020.5.86806\pythonFiles\lib\python\debugpy\no_wheels\debugpy/..\debugpy\server\cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "C:\Users\BOB\Documents\Python38\lib\runpy.py", line 265, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\BOB\Documents\Python38\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\BOB\Documents\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\BOB\Desktop\p.py", line 5, in <module>
    icon = pyglet.image.load("ship.png")
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\__init__.py", line 340, in __getattr__
    __import__(import_name)
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\image\__init__.py", line 2355, in <module>
    add_default_image_codecs()
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\image\codecs\__init__.py", line 215, in add_default_image_codecs     
    add_decoders(wic)
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\image\codecs\__init__.py", line 162, in add_decoders
    for decoder in module.get_decoders():
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\image\codecs\wic.py", line 414, in get_decoders
    return [WICDecoder()]
  File "C:\Users\BOB\Documents\Python38\lib\site-packages\pyglet\image\codecs\wic.py", line 292, in __init__
    warnings.warn(err)
TypeError: expected string or bytes-like object

所以,根本原因是 运行 在 numba 下,据我所知存在线程问题。 ole32.CoInitializeEx(None, COINIT_MULTITHREADED) 抛出 OSError 消息 [WinError -2147417850] Cannot change thread mode after it is set.

有一个修复它的新版本:https://github.com/pyglet/pyglet/releases/tag/v1.5.7

此错误被捕获:

try:
    ole32.CoInitializeEx(None, COINIT_MULTITHREADED)
except OSError as err:
    warnings.warn(err)

不幸的是 warnings.warn 需要一个字符串或字节对象才能打印它,这就是为什么你得到 TypeError: expected string or bytes-like object.

在与回购的主要维护者之一交谈后,我已经创建了对上游回购的拉取请求。您可以在此处关注进度:https://github.com/pyglet/pyglet/pull/230

之后您应该可以毫无问题地将 pyglet 更新到最新版本。虽然 运行 在 numba:

下会有警告信息
PS C:\Users\anton> python test.py
C:\Users\...\wic.py:292: UserWarning: [WinError -2147417850] Cannot change thread mode after it is set

希望这至少有助于澄清问题。