Gym's box 2d (openAI) 安装不成功(pip错误)

Gym's box 2d (openAI) doesn't install successfully (pip error)

我正在尝试使用 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: module 'gym.envs.box2d' has no attribute '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.

我需要做什么才能成功安装?

Windows 支持目前处于试验阶段 (source)。话虽这么说,在大多数情况下你会得到它的工作,但一些功能可能会被破坏。谨慎行事。

以下是可行的方法:

  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 安装基础健身房。
  6. (可选)pip install gym[all] - 安装 Gym 的所有组件。您可能会 运行 出错。

如果您在缺少编译器时遇到构建错误,请安装 Visual Studio Built Tools

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

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

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

这将从 powerai channel 安装 gym可能有效,但请注意它不是最新的,开发者似乎也不支持它。