openai / gym

A toolkit for developing and comparing reinforcement learning algorithms.
https://www.gymlibrary.dev
Other
34.6k stars 8.6k forks source link

[Question] There is no Box2D even though I pip installed gym #2795

Closed fehimornek closed 2 years ago

fehimornek commented 2 years ago

I am having trouble in a very simple task. I am trying to run CarRacing environment with the following code

import gym
env = gym.make("CarRacing-v1")
env.reset()

and it throws an error AttributeError: module 'gym.envs.box2d' has no attribute 'CarRacing' I tried to run another code that was proposed in another issue #2786 like this

from gym.envs.box2d.car_racing import CarRacing
env = CarRacing()
env.reset()

but this time got the error ModuleNotFoundError: No module named 'Box2D'

When I try to download Box2D with

pip install gym[box2d]

it says error: legacy-install-failure

and I searched online but solutions for gym doesnt really exist.

Thanks in advance.

pseudo-rnd-thoughts commented 2 years ago

I have never seen this error before Could you run pip uninstall gym and pip install gym[box2d] If this fail, could you give us the full install error message plus your operating system, the python version and the gym version that it tries to install.

sidney-tio commented 2 years ago

Hey both, I encountered the same problem trying to create LunarLander as well. It seems that the problem resides with Box2D and the related try statement that leaves the problem undetected at https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/box2d/__init__.py#L1-L2

When I tried pip install gym[box2d], it also resulted in same problems: error: legacy-install-failure.

I got around the problem by installing Box2D manually with pip install box2d pygame, and it works now.

@pseudo-rnd-thoughts: I noticed that gym's setup.py uses a deprecated version of Box2D (box2d-py). Perhaps the team would like to consider updating it to Box2D instead? https://pypi.org/project/Box2D/

sidney-tio commented 2 years ago

adding on: perhaps it would be useful to raise an error message when importing Box2D causes ImportErrors to help with debugging.

pseudo-rnd-thoughts commented 2 years ago

@sidney-tio Thanks for the comment, I had never seen that try and except and will see if I can remove that Investigating the box2d-py install, this seems to be a legacy produce of gym where OpenAI forked an old repo of box2d as the install from pip

I will investigate moving to the more recent box2d as a temporary solution as we planning on moving to brax which should improve performance for the environments however this could be a few months out at

fehimornek commented 2 years ago

Hi again, I tried both of the solutions and they didn't work, here are the error messages:

1. solution
pip uninstall gym
pip install gym[box2d]

Collecting gym[box2d] Using cached gym-0.23.1-py3-none-any.whl Requirement already satisfied: numpy>=1.18.0 in c:\users\fehim\pycharmprojects\cybernetics\venv\lib\site-packages (from gym[box2d]) (1.22.2) Requirement already satisfied: cloudpickle>=1.2.0 in c:\users\fehim\pycharmprojects\cybernetics\venv\lib\site-packages (from gym[box2d]) (2.0.0) Requirement already satisfied: importlib-metadata>=4.10.0 in c:\users\fehim\pycharmprojects\cybernetics\venv\lib\site-packages (from gym[box2d]) (4.11.3) Requirement already satisfied: gym-notices>=0.0.4 in c:\users\fehim\pycharmprojects\cybernetics\venv\lib\site-packages (from gym[box2d]) (0.0.6) Collecting box2d-py==2.3.5 Using cached box2d-py-2.3.5.tar.gz (374 kB) Preparing metadata (setup.py) ... done Collecting pygame==2.1.0 Using cached pygame-2.1.0-cp39-cp39-win_amd64.whl (4.8 MB) Requirement already satisfied: zipp>=0.5 in c:\users\fehim\pycharmprojects\cybernetics\venv\lib\site-packages (from importlib-metadata>=4.10.0->gym[box2d]) (3.8.0) Building wheels for collected packages: box2d-py Building wheel for box2d-py (setup.py) ... error error: subprocess-exited-with-error

× python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [16 lines of output] Using setuptools (version 57.0.0). running bdist_wheel running build running build_py creating build creating build\lib.win-amd64-3.9 creating build\lib.win-amd64-3.9\Box2D copying library\Box2D\Box2D.py -> build\lib.win-amd64-3.9\Box2D copying library\Box2D__init.py -> build\lib.win-amd64-3.9\Box2D creating build\lib.win-amd64-3.9\Box2D\b2 copying library\Box2D\b2\init__.py -> build\lib.win-amd64-3.9\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: None [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for box2d-py Running setup.py clean for box2d-py Failed to build box2d-py Installing collected packages: box2d-py, pygame, gym Running setup.py install for box2d-py ... error error: subprocess-exited-with-error

× Running setup.py install for box2d-py did not run successfully. │ exit code: 1 ╰─> [16 lines of output] Using setuptools (version 57.0.0). running install running build running build_py creating build creating build\lib.win-amd64-3.9 [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip. error: legacy-install-failure

× Encountered error while trying to install package. ╰─> box2d-py

note: This is an issue with the package mentioned above, not pip. hint: See above for output from the failure.

2. solution
pip install gym[box2d] pygame

Collecting box2d Using cached Box2D-2.3.2.tar.gz (427 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: pygame in c:\users\fehim\pycharmprojects\cybernetics\venv\lib\site-packages (2.1.2) Building wheels for collected packages: box2d Building wheel for box2d (setup.py) ... error error: subprocess-exited-with-error

× python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [16 lines of output] Using setuptools (version 57.0.0). running bdist_wheel running build running build_py creating build creating build\lib.win-amd64-3.9 creating build\lib.win-amd64-3.9\Box2D copying library\Box2D\Box2D.py -> build\lib.win-amd64-3.9\Box2D copying library\Box2D__init.py -> build\lib.win-amd64-3.9\Box2D creating build\lib.win-amd64-3.9\Box2D\b2 copying library\Box2D\b2\init__.py -> build\lib.win-amd64-3.9\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: None [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for box2d Running setup.py clean for box2d Failed to build box2d Installing collected packages: box2d Running setup.py install for box2d ... error error: subprocess-exited-with-error

× Running setup.py install for box2d did not run successfully. │ exit code: 1 ╰─> [16 lines of output] Using setuptools (version 57.0.0). running install running build running build_py creating build creating build\lib.win-amd64-3.9 creating build\lib.win-amd64-3.9\Box2D copying library\Box2D\Box2D.py -> build\lib.win-amd64-3.9\Box2D copying library\Box2D__init.py -> build\lib.win-amd64-3.9\Box2D creating build\lib.win-amd64-3.9\Box2D\b2 copying library\Box2D\b2\init__.py -> build\lib.win-amd64-3.9\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: None [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip. error: legacy-install-failure

× Encountered error while trying to install package. ╰─> box2d

note: This is an issue with the package mentioned above, not pip. hint: See above for output from the failure.

Sorry for the late reply

pseudo-rnd-thoughts commented 2 years ago

The error was caused by swig error: command 'swig.exe' failed: None You can download swig here This should fix your issue.

sidney-tio commented 2 years ago

if install swig doesnt work, try installing box2d manually (i.e. pip install box2d (instead of gym[box2d])? see my reply above:

Hey both, I encountered the same problem trying to create LunarLander as well. It seems that the problem resides with Box2D and the related try statement that leaves the problem undetected at

https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/box2d/__init__.py#L1-L2

When I tried pip install gym[box2d], it also resulted in same problems: error: legacy-install-failure.

I got around the problem by installing Box2D manually with pip install box2d pygame, and it works now.

fehimornek commented 2 years ago

After downloading swig I also had to

pip3 install box2d box2d-kengz

Solved the issue

But I don't understand why doing such a menial task required this many steps, what was the problem here me or the gym library?

pseudo-rnd-thoughts commented 2 years ago

So box2d is different from box2d-py that gym uses. You should just need to install swig (which is a requirement of box2d) and then run pip install gym[box2d]

Ashfaqe-oss commented 1 year ago

how am I supposed to install swig after downloading it ?

jjshoots commented 1 year ago

@Ashfaqe-oss In Ubuntu it should just be sudo apt install swig, on MacOS it should be sudo brew install swig, we don't have official Windows support.

kbbbhhy commented 1 year ago

Thanks. It works.

TernaryM01 commented 1 year ago

I've tried installing swig first: sudo apt install swig And only then install box2d: pip install gymnasium pip install gymnasium[box2d] It succeeded without any error or warning.

Louis1234567890 commented 1 year ago

When I uninstalled swig and reinstalled it I got

WARNING: The script swig.exe is installed in 'C:[...]\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Might this explain the (error: command 'swig.exe' failed: None) when installing gym[box2d] ?

jjshoots commented 1 year ago

@Louis1234567890, possibly, SWIG is a parser for Cpp to Python, so things could fail if that's not on PATH. That said, I've never developed code in Windows so I wouldn't know.

leiquan commented 1 year ago

After downloading swig I also had to

pip3 install box2d box2d-kengz

Solved the issue

But I don't understand why doing such a menial task required this many steps, what was the problem here me or the gym library?

thank you, it helps me

wsxyh107165243 commented 12 months ago

After downloading swig I also had to

pip3 install box2d box2d-kengz

Solved the issue

But I don't understand why doing such a menial task required this many steps, what was the problem here me or the gym library?

Thanks, this helped me.

khan008 commented 11 months ago

EveryOne, Instead of box2d install pip install pygame to get rid of this headache. Hope problem answered successfuly

Akram-Ashyani commented 6 months ago

Hey both, I encountered the same problem trying to create LunarLander as well. It seems that the problem resides with Box2D and the related try statement that leaves the problem undetected at

https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/box2d/__init__.py#L1-L2

When I tried pip install gym[box2d], it also resulted in same problems: error: legacy-install-failure.

I got around the problem by installing Box2D manually with pip install box2d pygame, and it works now.

@pseudo-rnd-thoughts: I noticed that gym's setup.py uses a deprecated version of Box2D (box2d-py). Perhaps the team would like to consider updating it to Box2D instead? https://pypi.org/project/Box2D/

Thank you immensely. Your solution was helpful and resolved my issue.

AsteroidHunter commented 5 months ago

If you ended up here because of the following error: zsh: no matches found: gymnasium[box2d] Try pip install 'gymnasium[box2d]'

Basically, Z shell interprets the square brackets as a pattern matching expression, and searches for files named "b" or "o" or "x" and so on. Enclosing it with quotations (' ') makes Zsh treat everything inside as a single string without any special characters.

danialzendehdel commented 4 months ago

If you ended up here because of the following error: zsh: no matches found: gymnasium[box2d] Try pip install 'gymnasium[box2d]'

Basically, Z shell interprets the square brackets as a pattern matching expression, and searches for files named "b" or "o" or "x" and so on. Enclosing it with quotations (' ') makes Zsh treat everything inside as a single string without any special characters.

This solved my problem

AbogAscended commented 1 month ago

If you ended up here because of the following error: zsh: no matches found: gymnasium[box2d] Try pip install 'gymnasium[box2d]'

Basically, Z shell interprets the square brackets as a pattern matching expression, and searches for files named "b" or "o" or "x" and so on. Enclosing it with quotations (' ') makes Zsh treat everything inside as a single string without any special characters.

I could kiss you