无法导入OpenGL.GL

问题描述 投票:0回答:1

设置

我在 WSL 中使用 Python 2.7.15rc1。我已经为我的项目设置了一个 virtualenv 环境。该项目需要 3 个外部包:numpy、Pillow 和 PyOpenGL。这是我的

的起始代码片段
import sys, os, numpy, math

try: # Pillow
  from PIL import Image
except:
  print 'Error: Pillow has not been installed.'
  sys.exit(0)

try: # PyOpenGL
  from OpenGL.GLUT import *
  from OpenGL.GL import *
  from OpenGL.GLU import *
except:
  print 'Error: PyOpenGL has not been installed.'
  sys.exit(0)

当然,没有安装任何外部包,代码没有运行。所以我安装了它们:

python -m pip install -U numpy
python -m pip install -U Pillow
python -m pip install -U PyOpenGL

问题

我遇到了 PyOpenGL 的

except
块(
Error: PyOpenGL has not been installed.
)。

我打开了 Python REPL 并尝试了这个:

import numpy
-> 作品

import Pillow
-> 作品

import OpenGL
-> 作品

from OpenGL.GL import *
-> 失败

对于失败的,我得到这个:

>>> from OpenGL.GL import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/GL/__init__.py", line 3, in <module>
    from OpenGL import error as _error
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/error.py", line 12, in <module>    from OpenGL import platform, _configflags
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/platform/__init__.py", line 35, in <module>
    _load()
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/platform/__init__.py", line 32, in _load
    plugin.install(globals())
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/platform/baseplatform.py", line 92, in install
    namespace[ name ] = getattr(self,name,None)
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/platform/baseplatform.py", line 14, in __get__
    value = self.fget( obj )
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/platform/glx.py", line 96, in GetCurrentContext
    return self.GL.glXGetCurrentContext
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/platform/baseplatform.py", line 14, in __get__
    value = self.fget( obj )
  File "/mnt/c/Users/Henry/Desktop/cmpe457/a1/local/lib/python2.7/site-packages/OpenGL/platform/glx.py", line 20, in GL
    raise ImportError("Unable to load OpenGL library", *err.args)
ImportError: ('Unable to load OpenGL library', 'GL: cannot open shared object file: No such file or directory', 'GL',
None)

知道哪里出了问题吗?

python virtualenv pyopengl
1个回答
0
投票

网上到处都是这个问题。答案是进口必须以正确的顺序:

import OpenGL
import OpenGL.GLU
import OpenGL.GL

至少对于 python3.5python3-opengl 版本 3.0.2,如果 GL 导入在 GLU 导入之前出现,将会出现一连串错误消息,结尾为:

AttributeError:模块“OpenGL.GL”没有属性“GL_READ_WRITE”。
大概这只是另一个晦涩的 python 版本可比性问题。

© www.soinside.com 2019 - 2024. All rights reserved.