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

Question:

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:UsersJuniorAnacondaenvsgympython.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('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UsersJuniorAppDataLocalTemppip-record-netg1nlqinstall-record.txt' --single-version-externally-managed --compile --install-headers 'C:UsersJuniorAnacondaenvsgymIncludebox2d-py'
         cwd: C:UsersJuniorAppDataLocalTemppip-install-w8awn22pbox2d-py
    Complete output (16 lines):
    Using setuptools (version 45.2.0.post20200210).
    running install
    running build
    running build_py
    creating build
    creating buildlib.win-amd64-3.7
    creating buildlib.win-amd64-3.7Box2D
    copying libraryBox2DBox2D.py -> buildlib.win-amd64-3.7Box2D
    copying libraryBox2D__init__.py -> buildlib.win-amd64-3.7Box2D
    creating buildlib.win-amd64-3.7Box2Db2
    copying libraryBox2Db2__init__.py -> buildlib.win-amd64-3.7Box2Db2
    running build_ext
    building 'Box2D._Box2D' extension
    swigging Box2DBox2D.i to Box2DBox2D_wrap.cpp
    swig.exe -python -c++ -IBox2D -small -O -includeall -ignoremissing -w201 -globals b2Globals -outdir libraryBox2D -keyword -w511 -D_SWIG_KWARGS -o Box2DBox2D_wrap.cpp Box2DBox2D.i
    error: command 'swig.exe' failed: No such file or directory
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:UsersJuniorAnacondaenvsgympython.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('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UsersJuniorAppDataLocalTemppip-record-netg1nlqinstall-record.txt' --single-version-externally-managed --compile --install-headers 'C:UsersJuniorAnacondaenvsgymIncludebox2d-py' Check the logs for full command output.

what I have to do to install it successfully?

Asked By: user12096782

||

Answers:

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.

Answered By: Lukasz Tracewski

enter image description here

I got it sorted with just installing swig before gym[box2d] no anaconda needed

pip3 install swig

In my case, I used the conda to install Gym’s Box2D:

conda install swig
conda install -c conda-forge gym-box2d

and got rid of this error.

Answered By: Isteak

(As a windows user) For me, the solution was to download and install the latest stable version of Build Tools pour Visual Studio (2022 currently). Then the pip install gym[box2d] worked.

Answered By: Clément Perroud