Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fehimornek opened this issue May 1, 2022 · 22 comments
Closed

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

fehimornek opened this issue May 1, 2022 · 22 comments

Comments

@fehimornek
Copy link

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
Copy link
Contributor

pseudo-rnd-thoughts commented May 1, 2022

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
Copy link

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

try:
import Box2D

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
Copy link

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

@pseudo-rnd-thoughts
Copy link
Contributor

@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
Copy link
Author

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
Copy link
Contributor

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

@sidney-tio
Copy link

sidney-tio commented May 11, 2022

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

try:
import Box2D

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
Copy link
Author

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
Copy link
Contributor

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
Copy link

how am I supposed to install swig after downloading it ?

@jjshoots
Copy link
Contributor

@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
Copy link

kbbbhhy commented Oct 22, 2022

Thanks. It works.

@TernaryM01
Copy link

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
Copy link

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
Copy link
Contributor

jjshoots commented Mar 7, 2023

@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
Copy link

leiquan commented Jul 11, 2023

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
Copy link

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
Copy link

khan008 commented Oct 18, 2023

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

@Akram-Ashyani
Copy link

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

try:
import Box2D

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
Copy link

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
Copy link

danialzendehdel commented Jun 4, 2024

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
Copy link

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

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests