CentOS 7.5运行Gym代码报GLU库缺失ImportError问题求助
Problem Context
You're trying to run a Gym CartPole-v0 script on CentOS 7.5 with Python 3, but hitting a GLU library not found error even after installing python-opengl via YUM. Here's your setup and error details:
Your Test Code
import gym import time env = gym.make('CartPole-v0') observation = env.reset() count = 0 for t in range(100): action = env.action_space.sample() observation, reward, done, info = env.step(action) if done: break env.render() count+=1 time.sleep(0.2)
Error Traceback
Traceback (most recent call last): File "<stdin>", line 6, in <module> File "/root/gym/gym/core.py", line 284, in render return self.env.render(mode) File "/root/gym/gym/envs/classic_control/cartpole.py", line 106, in render from gym.envs.classic_control import rendering File "/root/gym/gym/envs/classic_control/rendering.py", line 25, in <module> reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'") File "/root/gym/gym/utils/reraise.py", line 17, in reraise reraise_impl(e, traceback) File "/root/gym/gym/utils/reraise_impl_py3.py", line 4, in reraise_impl raise e.with_traceback(traceback) from None File "/root/gym/gym/envs/classic_control/rendering.py", line 23, in <module> from pyglet.gl import * File "/root/anaconda3/envs/gymlab/lib/python3.6/site-packages/pyglet/gl/__init__.py", line 100, in <module> from pyglet.gl.lib import GLException File "/root/anaconda3/envs/gymlab/lib/python3.6/site-packages/pyglet/gl/lib.py", line 143, in <module> from pyglet.gl.lib_glx import link_GL, link_GLU, link_GLX File "/root/anaconda3/envs/gymlab/lib/python3.6/site-packages/pyglet/gl/lib_glx.py", line 51, in <module> glu_lib = pyglet.lib.load_library('GLU') File "/root/anaconda3/envs/gymlab/lib/python3.6/site-packages/pyglet/lib.py", line 158, in load_library raise ImportError('Library "%s" not found.' % names[0]) gym.utils.reraise.ReraisedException: Error occured while running `from pyglet.gl import *`
Original Root Error
ImportError: Library "GLU" not found.
HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s "-screen 0 1400x900x24" python <your_script.py>'
Your Failed Installation Attempt
[root@devmaster ~]# yum -y install python-opengl Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.tuna.tsinghua.edu.cn * updates: ftp.iij.ad.jp Package PyOpenGL-3.0.1-6.el7.noarch already installed and latest version Nothing to do [root@devmaster ~]#
Solution
The issue here is that python-opengl is just the Python binding layer—you're missing the system-level OpenGL/GLU libraries that PyOpenGL relies on. Plus, if you're running on a headless server (no physical display), you'll need a virtual frame buffer to render the GUI.
Follow these steps to fix it:
Install System-Level OpenGL Libraries
Install the missing GL and GLU libraries via YUM:yum install -y mesa-libGLU mesa-libGLThese packages provide the low-level system libraries that PyOpenGL needs to link against, which is what was missing even after installing the Python package.
Set Up Virtual Frame Buffer (For Headless Servers)
If you're running on a server without a display, you need Xvfb to create a virtual display environment for the rendering to work:yum install -y xorg-x11-server-XvfbRun your script directly with Xvfb:
xvfb-run -s "-screen 0 1400x900x24" python your_script.pyFor an interactive Python session:
# Start Xvfb in the background Xvfb :1 -screen 0 1400x900x24 & # Point the DISPLAY variable to the virtual screen export DISPLAY=:1 # Now launch your Python shell or run the code pythonVerify the Fix
After installing the libraries and setting up the virtual display (if needed), re-run your CartPole script—it should now render without the GLU library error.
内容的提问来源于stack exchange,提问作者Jackon

