健身房的Box2D(OpenAI)安装不成功(pip错误)

3

我正在尝试使用OpenAI编写以下代码:

import gym
env = gym.make('CarRacing-v0')
env.reset() 

for _ in range(1000):
 env.render()
 env.step(env.action_space.sample())

但是它抛出了以下错误:

fn = getattr(mod, attr_name) AttributeError: 模块 'gym.envs.box2d' 没有属性 'CarRacing'

然后我尝试通过 pip install box2d-py 安装 box2d,但是抛出了以下错误:

ERROR: Command errored out with exit status 1:
     command: 'C:\Users\Junior\Anaconda\envs\gym\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Junior\\AppData\\Local\\Temp\\pip-install-w8awn22p\\box2d-py\\setup.py'"'"'; __file__='"'"'C:\\Users\\Junior\\AppData\\Local\\Temp\\pip-install-w8awn22p\\box2d-py\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Junior\AppData\Local\Temp\pip-record-netg1nlq\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\Junior\Anaconda\envs\gym\Include\box2d-py'
         cwd: C:\Users\Junior\AppData\Local\Temp\pip-install-w8awn22p\box2d-py\
    Complete output (16 lines):
    Using setuptools (version 45.2.0.post20200210).
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.7
    creating build\lib.win-amd64-3.7\Box2D
    copying library\Box2D\Box2D.py -> build\lib.win-amd64-3.7\Box2D
    copying library\Box2D\__init__.py -> build\lib.win-amd64-3.7\Box2D
    creating build\lib.win-amd64-3.7\Box2D\b2
    copying library\Box2D\b2\__init__.py -> build\lib.win-amd64-3.7\Box2D\b2
    running build_ext
    building 'Box2D._Box2D' extension
    swigging Box2D\Box2D.i to Box2D\Box2D_wrap.cpp
    swig.exe -python -c++ -IBox2D -small -O -includeall -ignoremissing -w201 -globals b2Globals -outdir library\Box2D -keyword -w511 -D_SWIG_KWARGS -o Box2D\Box2D_wrap.cpp Box2D\Box2D.i
    error: command 'swig.exe' failed: No such file or directory
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Users\Junior\Anaconda\envs\gym\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Junior\\AppData\\Local\\Temp\\pip-install-w8awn22p\\box2d-py\\setup.py'"'"'; __file__='"'"'C:\\Users\\Junior\\AppData\\Local\\Temp\\pip-install-w8awn22p\\box2d-py\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Junior\AppData\Local\Temp\pip-record-netg1nlq\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\Junior\Anaconda\envs\gym\Include\box2d-py' Check the logs for full command output.

我该怎么做才能成功安装它?

它需要 swig.exe,所以你可能需要 http://www.swig.org/index.php - furas
你尝试过使用conda吗? - Lukasz Tracewski
2
在 Mac 上,只需运行 pip install Box2d 即可。这对我有效。 - Roy
4个回答

5

目前,Windows的支持属于实验性质(来源)。尽管在大多数情况下,您可以使其正常工作,但某些功能可能会出现问题,请谨慎使用。

以下是可能有效的步骤:

  1. 安装Anaconda
  2. 启动Anaconda命令行(它会将conda添加到PATH中)。
  3. conda create -c conda-forge -n gymenv swig pip - 创建基础环境。
  4. conda activate gymenv - 激活该环境。
  5. pip install Box2D gym - 安装带有Box2D的基础gym。
  6. (可选) pip install gym[all] - 安装Gym所有组件。您可能会遇到错误。

如果缺少编译器导致构建错误,请安装Visual Studio Build Tools

根据您使用的组件不同,您可能需要例如pystan。然后激活conda环境并安装它,即conda install pystan

另一种方法是尝试直接通过conda安装Gym:

conda create -c conda-forge -c powerai -n gymenv gym swig pip pystan

这将从 powerai 渠道 安装 gym。可能可以工作,但请注意它不是最新版本,开发人员似乎也不支持它。

在 Mac 上,只需运行 pip install Box2d 即可。这对我有效。 - Roy

4

enter image description here

我只需安装swig就能解决问题,无需使用anaconda,这与gym[box2d]相关。
pip3 install swig

2
在我的情况下,我使用了conda来安装Gym的Box2D:
conda install swig

conda install -c conda-forge gym-box2d

并且解决了这个错误。

0

(作为Windows用户)对我来说,解决方案是下载并安装最新稳定版本的Visual Studio Build Tools(目前是2022版)。然后pip install gym[box2d]就可以正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接