无法opengl.gl在macos上导入python中的www.example.com

ru9i0ody  于 2022-11-04  发布在  Mac
关注(0)|答案(5)|浏览(182)

我正在用OpenGL渲染python中的一个场景。我的代码在Windows上运行得很好,但是,由于某种原因,我在MacOS上导入www.example.com时遇到了问题opengl.gl。
在python脚本和python控制台中调用from OpenGL.GL import ...时会出现此问题。
更具体地说,下面是我脚本中的确切调用:

from OpenGL.GL import glGenBuffers, glBindBuffer, glBufferData, \
    glGenVertexArrays, glBindVertexArray, glEnableVertexAttribArray, glVertexAttribPointer, \
    glDrawArrays, glUseProgram, glEnable, glDisable, \
    GL_ARRAY_BUFFER, GL_STATIC_DRAW, GL_DEPTH_TEST, \
    GL_FLOAT, GL_FALSE, \
    GL_TRIANGLES, GL_LINES, GL_LINE_STRIP

这将导致以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/darwin.py", line 35, in GL
    return ctypesloader.loadLibrary(
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/ctypesloader.py", line 36, in loadLibrary
    return _loadLibraryWindows(dllType, name, mode)
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/ctypesloader.py", line 89, in _loadLibraryWindows
    return dllType( name, mode )
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ctypes/__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ('dlopen(OpenGL, 10): image not found', 'OpenGL', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/iyadboustany/Desktop/lensmaster/Lensmaster.py", line 18, in <module>
    from MainWindow import MainWindow
  File "/Users/iyadboustany/Desktop/lensmaster/MainWindow.py", line 14, in <module>
    from Robot import Robot
  File "/Users/iyadboustany/Desktop/lensmaster/Robot.py", line 8, in <module>
    from Graphics.Scene import DHNode
  File "/Users/iyadboustany/Desktop/lensmaster/Graphics/Scene.py", line 13, in <module>
    from OpenGL.GL import glGenBuffers, glBindBuffer, glBufferData, \
  File "/usr/local/lib/python3.8/site-packages/OpenGL/GL/__init__.py", line 3, in <module>
    from OpenGL import error as _error
  File "/usr/local/lib/python3.8/site-packages/OpenGL/error.py", line 12, in <module>
    from OpenGL import platform, _configflags
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/__init__.py", line 36, in <module>
    _load()
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/__init__.py", line 33, in _load
    plugin.install(globals())
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/baseplatform.py", line 97, in install
    namespace[ name ] = getattr(self,name,None)
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/baseplatform.py", line 15, in __get__
    value = self.fget( obj )
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/darwin.py", line 62, in GetCurrentContext
    return self.CGL.CGLGetCurrentContext 
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/baseplatform.py", line 15, in __get__
    value = self.fget( obj )
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/darwin.py", line 45, in CGL
    def CGL(self): return self.GL
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/baseplatform.py", line 15, in __get__
    value = self.fget( obj )
  File "/usr/local/lib/python3.8/site-packages/OpenGL/platform/darwin.py", line 41, in GL
    raise ImportError("Unable to load OpenGL library", *err.args)
ImportError: ('Unable to load OpenGL library', 'dlopen(OpenGL, 10): image not found', 'OpenGL', None)

备注

  • 运行glxgears工作得很好。
  • 我正在运行macOS Big Sur测试版(20A5343i)
  • 我用的是python 3.8.5
  • 我使用pip安装了opengl:pip3 install PyOpenGL PyOpenGL_accelerate
ndh0cuux

ndh0cuux1#

这个错误是因为Big Sur在文件系统的标准位置不再有OpenGL库或其他系统库,而是使用缓存。PyOpenGL使用ctypes来尝试定位OpenGL库,但它没有找到。在Python中修复ctypes,以便它会找到此拉取请求的主题库
https://github.com/python/cpython/pull/21241
所以Python的未来版本应该可以解决这个问题。
要修复它,现在你可以编辑PyOpenGL文件OpenGL/platform/ctypesloader.py更改行

fullName = util.find_library( name )

fullName = '/System/Library/Frameworks/OpenGL.framework/OpenGL'
li9yvcax

li9yvcax2#

因为我 * 讨厌 * 修补Python包的想法(而且使用Conda来安装这个包真的很难做到),所以我提供了这个杂七杂八的解决方案,直到Python得到它的Big Sur(又名Big Nuisance)更新:

try:
    import OpenGL as ogl
    try:
        import OpenGL.GL   # this fails in <=2020 versions of Python on OS X 11.x
    except ImportError:
        print('Drat, patching for Big Sur')
        from ctypes import util
        orig_util_find_library = util.find_library
        def new_util_find_library( name ):
            res = orig_util_find_library( name )
            if res: return res
            return '/System/Library/Frameworks/'+name+'.framework/'+name
        util.find_library = new_util_find_library
except ImportError:
    pass
wtlkbnrh

wtlkbnrh3#

最好将其更改为

fullName = "/System/Library/Frameworks/{}.framework/{}".format(name,name)

否则GLUT将无法工作。至少,这让我可以再次运行Cozmo SDK的3d功能。

0mkxixxg

0mkxixxg4#

顺便说一句,我的情况是:

fullName = '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/OpenGL'
r1zhe5dt

r1zhe5dt5#

这些都不是真正非常合适的答案-MacOS和pyOpenGL都有几个问题。
然而幸运的是--如果我们升级到Python3.10(或更高版本),一切似乎终于都正常了

相关问题