15

i want to play with the lunar lander env from OpenAI gym.

In order to run this code I need to install Box2d, this is where my problems arise. I am using ubuntu 16.04 with conda 4.3.21 and python 3.6. When I tried to run the environment I received the error: ModuleNotFoundError: No module named '_Box2D'

So I tried the direct install of pybox2d: https://github.com/pybox2d/pybox2d/blob/master/INSTALL.md which yielded the same error message.

Then I tried to install from GitHub following the way outlined in https://github.com/cbfinn/gps/issues/34

$git clone https://github.com/pybox2d/pybox2d pybox2d_dev
$cd pybox2d_dev
$python setup.py build 
$sudo python setup.py install

If I run this (in root environment which has python 3 or another new created environment with python 3) i get the result:

a lot of processing logs Processing Box2D-2.3.2-py2.7-linux-x86_64.egg creating /usr/local/lib/python2.7/dist-packages/Box2D-2.3.2-py2.7-linux-x86_64.egg Extracting Box2D-2.3.2-py2.7-linux-x86_64.egg to /usr/local/lib/python2.7/dist-packages Adding Box2D 2.3.2 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/Box2D-2.3.2-py2.7-linux-x86_64.egg Processing dependencies for Box2D==2.3.2 Finished processing dependencies for Box2D==2.3.2

So pybox2d is installed into the lib of the standard python 2 of ubuntu despite being in a python 3 conda environment.

So, I am looking for ways to install the pybox2d package for python 3 with conda 4.3.21

wonea
  • 4,783
  • 17
  • 86
  • 139
R. M
  • 161
  • 1
  • 1
  • 6

5 Answers5

35

Installing Box2D from pip led me to the error described here when I tried to import it. Here's what worked for me on Python 3.6, as suggested in that GitHub issue:

conda install swig # needed to build Box2D in the pip install
pip install box2d-py # a repackaged version of pybox2d
Nathan
  • 9,651
  • 4
  • 45
  • 65
9

Hey this question looks quite old but it seems no one really put the right answer in any where so just write this.

Follow the below two lines on your linux command:

$ sudo apt-get install build-essential python-dev swig python-pygame

$ pip install Box2D

---------Below is unnecessary details --------

Many wants to run Box2D based gym but it is sth you have to install by yourself like Mujoco series gym envs.

Many uses python 3.6 but the easiest way of installing Box2D, which is $ conda install -c kne pybox2d doesn't work cuz pybox2d has been maintained til py3.5

But directly doing $ pip install Box2D does not solve the issue. The error comes from swig given its error msg, but it's actually not. $ sudo apt-get install build-essential python-dev swig python-pygame This line always solve everything in one go.

I have been doing research based on Box2D envs but still foget this everytime I install this in a new env, so this is for me as well lol

Contribute: https://github.com/jonasschneider/box2d-py/blob/master/INSTALL.md

Btw, do not forget to double-check whether it is properly install. In linux command

$ python --version
python 3.6. sthsth
$ python
>>> import numpy as np
>>> import gym
>>> env = gym.make('BipedalWalker-v2')
# If it does not give you error, then it's done!
sdr2002
  • 542
  • 2
  • 6
  • 16
  • Btw, do not forget to double-check whether it is properly install. – sdr2002 Jun 30 '18 at 06:34
  • It did not give me an error, but a warning: WARN: gym.spaces.Box autodetected dtype as . Please provide explicit dtype. – Hermes Morales Mar 27 '21 at 11:39
  • How do I install the same things using windows? – Martin Feb 18 '22 at 20:16
  • 1
    @Martin the best answer would be to run ubuntu os on windows in these days. Do not try to setup your default dev environments primarily on windows unless you are comfortable on what you have been doing. Try WSL2. This will give you Ubuntu OS screen-session on your window. – sdr2002 Dec 10 '22 at 18:39
  • 1
    @HermesMorales it is a warning not relevant to the gym itself, but sometimes it requires you to specify some data types of related modules like numpy. As is not specified by you, gym chooses a reasonable type as np.float32 out of np.float64, and etc. You may not need to worry about that for this case at this moment. – sdr2002 Dec 10 '22 at 18:43
0

You need to activate your environment:

source activate my_env_name

Then prompt changes to:

(my_env_name)

Now, install with pip and without sudo:

pip install pybox2d
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
0
sudo apt-get install swig

pip install pybox2d
Tim Kretschmer
  • 2,272
  • 1
  • 22
  • 35
0

Installing this way worked for me (also in a virtual env):

pip install box2d-py==2.3.8

I am running gym v 0.17.3.