告诉我为什么在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
。该错误被捕获:
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
希望这至少可以帮助解决该问题。