You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS 7.5运行Gym代码报GLU库缺失ImportError问题求助

Fixing "ImportError: Library 'GLU' not found" in CentOS 7.5 for Gym CartPole

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:

  1. Install System-Level OpenGL Libraries
    Install the missing GL and GLU libraries via YUM:

    yum install -y mesa-libGLU mesa-libGL
    

    These packages provide the low-level system libraries that PyOpenGL needs to link against, which is what was missing even after installing the Python package.

  2. 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-Xvfb
    

    Run your script directly with Xvfb:

    xvfb-run -s "-screen 0 1400x900x24" python your_script.py
    

    For 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
    python
    
  3. Verify 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:26:20