Search code examples
pythonmachine-learningbox2dreinforcement-learningopenai-gym

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


I'm trying to do the follow code with OpenAI:

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

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

but it throws the error:

fn = getattr(mod, attr_name) AttributeError: module 'gym.envs.box2d' has no attribute 'CarRacing'

And then I try to install box2d by pip install box2d-py throwing this error:

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.

what I have to do to install it successfully?


Solution

  • Windows support is at present moment experimental (source). That being said, on most of the occasions you will get it to work, but some of the functionality could be broken. Proceed with caution.

    Here's what could work:

    1. Install Anaconda.
    2. Start Anaconda command line (it adds conda to the PATH).
    3. conda create -c conda-forge -n gymenv swig pip - create base environment.
    4. conda activate gymenv - activate it.
    5. pip install Box2D gym - install base gym with Box2D.
    6. (Optional) pip install gym[all] - install all components of Gym. You might run into errors.

    If you get build errors on missing compilers, install Visual Studio Built Tools.

    Depending on the component you're using, you might need e.g. pystan. Activate then the conda environment and install it, i.e. conda install pystan.

    An alternative could be to try to install Gym directly through conda:

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

    This will install gym from powerai channel. Might work, but mind that it's not the latest nor seems to be supported by devs.