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

Gym env.render()引发内核崩溃及GL驱动加载失败问题求助

问题说明
  • 环境:Python 3.10.6 + gym==0.26.0(conda环境)
  • VSCode中Jupyter Notebook问题:
    • 注释env.render()时代码正常执行,取消注释后内核直接崩溃,提示内核崩溃相关错误
  • 普通.py文件问题:
    • 当设置render_mode="human"并调用env.reset()时,抛出OpenGL驱动加载失败错误(找不到iris_dri.so、swrast_dri.so文件);移除render_mode参数或不调用env.reset()则无报错
解决步骤
  1. 修复OpenGL驱动依赖
    • Debian/Ubuntu系统执行:
      sudo apt install libgl1-mesa-glx libgl1-mesa-dri
      
    • Fedora/RHEL系统执行:
      sudo dnf install mesa-libGL mesa-dri-drivers
      
    • 验证修复:运行glxinfo | grep "OpenGL version",能正常输出版本信息说明驱动没问题
  2. 调整Jupyter Notebook中的渲染方式
    直接用render_mode="human"会导致内核崩溃,改用rgb_array模式配合matplotlib显示画面:
    import gym
    import matplotlib.pyplot as plt
    
    env = gym.make('CarRacing-v2', render_mode='rgb_array')
    observation = env.reset()
    for _ in range(100):
        state, reward, terminated, _, info = env.step(env.action_space.sample())
        frame = env.render()
        plt.imshow(frame)
        plt.pause(0.01)
        plt.clf()
        if terminated:
            break
    env.close()
    
  3. 普通.py文件的渲染适配
    • 本地环境:驱动修复后直接运行代码即可
    • 远程服务器(无图形界面):用虚拟显示工具规避X11依赖
      先安装依赖:
      pip install pyvirtualdisplay
      
      修改代码:
      from pyvirtualdisplay import Display
      # 创建虚拟显示
      display = Display(visible=0, size=(1400, 900))
      display.start()
      
      import gym
      env = gym.make("CarRacing-v2", render_mode="human")
      observation, info = env.reset()
      # 后续逻辑代码
      env.close()
      # 关闭虚拟显示
      display.stop()
      
      或者用命令行直接启动:
      xvfb-run -a python your_script.py
      
  4. 补全gym环境依赖
    CarRacing属于box2d类环境,确保依赖完整:
    pip install gym[box2d]
    

内容的提问来源于stack exchange,提问作者brownie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 06:40:35