Vladimir
Vladimir

Reputation: 13

error while import numba module in pyglet projects

Tell me why if I import the numba module in the pyglet project, I get an error when loading images or sound files.

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()

And I get the following error:

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

Upvotes: 1

Views: 845

Answers (1)

Torxed
Torxed

Reputation: 23480

So, the root cause of this is that while running under numba there's a threading issue as far as I could tell. ole32.CoInitializeEx(None, COINIT_MULTITHREADED) throws an OSError with the message [WinError -2147417850] Cannot change thread mode after it is set.

There's a new version out which fixes it: https://github.com/pyglet/pyglet/releases/tag/v1.5.7

This error got caught by:

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

Unfortunately warnings.warn expects a string or bytes object in order to print it, which is why you were getting TypeError: expected string or bytes-like object.

I've created a pull request to the upstream repo after talking with one of the main maintainer of the repo. You can follow the progress along here: https://github.com/pyglet/pyglet/pull/230

After which you should probably be able to update pyglet to the latest without any issues. Altho there will be a warning message while running under numba:

PS C:\Users\anton> python test.py
C:\Users\...\wic.py:292: UserWarning: [WinError -2147417850] Cannot change thread mode after it is set

Hope this helps and clarifies the issue at least.

Upvotes: 1

Related Questions