无法导入 OpenGL.GL



Setup

我在WSL中使用Python 2.7.15rc1。我已经为我的项目设置了一个虚拟环境。这个项目需要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)

知道出了什么问题吗?

这个问题在互联网上到处都是。答案是导入必须按正确的顺序排列:

import OpenGL
import OpenGL.GLU
import OpenGL.GL

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

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

最新更新