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

在JupyterLab搭建gym_super_mario_bros环境时遭遇OverflowError

解决gym_super_mario_bros环境搭建中的OverflowError问题

问题场景

我正在跟随YouTube教程使用Python构建Super Mario AI模型,但在搭建gym_super_mario_bros环境时遇到了OverflowError。使用的代码如下:

# Import the game
import gym_super_mario_bros
# Import the Joypad wrapper
from nes_py.wrappers import JoypadSpace
# Import the SIMPLIFIED controls
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT

# Setup game
env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)

错误信息

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Cell In[3], line 2
      1 # Setup game
----> 2 env = gym_super_mario_bros.make('SuperMarioBros-v0')
      3 env = JoypadSpace(env, SIMPLE_MOVEMENT)

File /Volumes/-/mario ai/mario/lib/python3.10/site-packages/gym/envs/registration.py:592, in make(id, max_episode_steps, autoreset, disable_env_checker, **kwargs)
    588 else:
    589     # Assume it's a string
    590     env_creator = load(spec_.entry_point)
---> 592 env = env_creator(**_kwargs)
    594 # Copies the environment creation specification and kwargs to add to the environment specification details
    595 spec_ = copy.deepcopy(spec_)

File /Volumes/-/mario ai/mario/lib/python3.10/site-packages/gym_super_mario_bros/smb_env.py:52, in SuperMarioBrosEnv.__init__(self, rom_mode, lost_levels, target)
     50 rom = rom_path(lost_levels, rom_mode)
     51 # initialize the super object with the ROM path
---> 52 super(SuperMarioBrosEnv, self).__init__(rom)
     53 # set the target world, stage, and area variables
     54 target = decode_target(target, lost_levels)

File /Volumes/-/mario ai/mario/lib/python3.10/site-packages/nes_py/nes_env.py:126, in NESEnv.__init__(self, rom_path)
    124     raise ValueError('ROM has trainer. trainer is not supported.')
    125 # try to read the PRG ROM and raise a value error if it fails
---> 126 _ = rom.prg_rom
    127 # try to read the CHR ROM and raise a value error if it fails
    128 _ = rom.chr_rom

File /Volumes/-/mario ai/mario/lib/python3.10/site-packages/nes_py/_rom.py:204, in ROM.prg_rom(self)
    202 """Return the PRG ROM of the ROM file."""
    203 try:
---> 204     return self.raw_data[self.prg_rom_start:self.prg_rom_stop]
    205 except IndexError:
    206     raise ValueError('failed to read PRG-ROM on ROM.')

File /Volumes/-/mario ai/mario/lib/python3.10/site-packages/nes_py/_rom.py:198, in ROM.prg_rom_stop(self)
    195 @property
    196 def prg_rom_stop(self):
    197     """The exclusive stopping index of the PRG ROM."""
---> 198     return self.prg_rom_start + self.prg_rom_size * 2**10

OverflowError: Python integer 1024 out of bounds for uint8

环境信息:Python 3.10,最新版gym_super_mario_bros,运行在Mac的JupyterLab中。

错误原因

该错误源于nes_py库的_rom.py文件存在类型处理bug:计算prg_rom_stop时,self.prg_rom_size是uint8类型(取值范围0-255),乘以1024后结果超出了uint8的范围,导致溢出。这个问题在ARM架构的Mac(如M系列芯片)上更容易触发。

解决方案

方案一:降级nes_py版本

卸载当前版本的nes_py,安装修复了该问题的指定版本:

pip uninstall nes_py -y
pip install nes_py==8.2.1

方案二:手动修改nes_py源码

找到nes_py安装目录下的_rom.py文件(路径类似/你的虚拟环境路径/lib/python3.10/site-packages/nes_py/_rom.py),修改两处属性的计算逻辑:

  1. 修改prg_rom_stop:
@property
def prg_rom_stop(self):
    """The exclusive stopping index of the PRG ROM."""
    return self.prg_rom_start + int(self.prg_rom_size) * 2**10
  1. 修改chr_rom_stop:
@property
def chr_rom_stop(self):
    """The exclusive stopping index of the CHR ROM."""
    return self.chr_rom_start + int(self.chr_rom_size) * 2**10

修改后保存文件,重新运行代码即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 18:13:14