box2D is not installed, run `pip install gym[box2d]`

Hi there

I’m trying to run C3_W3_A1 assignement on my local mac jupyter notebook and getting the following error when executing this line of code env = gym.make(‘LunarLander-v2’) .

Error: box2D is not installed, run pip install gym[box2d]

When trying to install gym[box2d] I get a number of build errors linked to the installation of pygame dependencies.

These are the steps taken so far without much success:

  1. Updated Xcode Command Line Tools using xcode-select --install.
  2. Upgraded pip and setuptools to the latest versions.
  3. Installed system dependencies (sdl2, sdl2_image, sdl2_ttf, sdl2_mixer) using Homebrew.
  4. Attempted to install pygame separately using pip install --no-cache-dir pygame.
  5. Retried installation of gym[box2d] using pip install --no-cache-dir "gym[box2d]".

Any ideas on what might be going on?

Can you share the screenshot of the image error about box-2d

Hi Deepti

Here the jupyter notebook error:

--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gym/envs/box2d/bipedal_walker.py:14 13 try: —> 14 import Box2D 15 from Box2D.b2 import ( 16 circleShape, 17 contactListener, (…) 21 revoluteJointDef, 22 ) File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/Box2D/init.py:20 1 # 2 # 3 # C++ version copyright 2010 Erin Catto http://www.gphysics.com (…) 18 # 3. This notice may not be removed or altered from any source distribution. 19 # —> 20 from .Box2D import * 21 author = ‘Date’ File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/Box2D/Box2D.py:32 31 return mod —> 32 Box2D = swig_import_helper() 33 del swig_import_helper File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/Box2D/Box2D.py:19, in swig_import_helper() 18 from os.path import dirname —> 19 import imp 20 fp = None ModuleNotFoundError: No module named ‘imp’ During handling of the above exception, another exception occurred: DependencyNotInstalled Traceback (most recent call last) Cell In[8], line 1 ----> 1 env = gym.make(‘LunarLander-v2’) File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gym/envs/registration.py:581, in make(id, max_episode_steps, autoreset, apply_api_compatibility, disable_env_checker, **kwargs) 578 env_creator = spec.entry_point 579 else: 580 # Assume it’s a string → 581 env_creator = load(spec.entry_point) 583 mode = _kwargs.get(“render_mode”) 584 apply_human_rendering = False File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gym/envs/registration.py:61, in load(name) 52 “”“Loads an environment with name and returns an environment creation function 53 54 Args: (…) 58 Calls the environment constructor 59 “”” 60 mod_name, attr_name = name.split(“:”) —> 61 mod = importlib.import_module(mod_name) 62 fn = getattr(mod, attr_name) 63 return fn File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/init.py:90, in import_module(name, package) 88 break 89 level += 1 —> 90 return _bootstrap._gcd_import(name[level:], package, level) File :1387, in _gcd_import(name, package, level) File :1360, in find_and_load(name, import) File :1310, in find_and_load_unlocked(name, import) File :488, in _call_with_frames_removed(f, *args, **kwds) File :1387, in _gcd_import(name, package, level) File :1360, in find_and_load(name, import) File :1331, in find_and_load_unlocked(name, import) File :935, in _load_unlocked(spec) File :995, in exec_module(self, module) File :488, in _call_with_frames_removed(f, *args, **kwds) File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gym/envs/box2d/init.py:1 ----> 1 from gym.envs.box2d.bipedal_walker import BipedalWalker, BipedalWalkerHardcore 2 from gym.envs.box2d.car_racing import CarRacing 3 from gym.envs.box2d.lunar_lander import LunarLander, LunarLanderContinuous File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gym/envs/box2d/bipedal_walker.py:24 15 from Box2D.b2 import ( 16 circleShape, 17 contactListener, (…) 21 revoluteJointDef, 22 ) 23 except ImportError: —> 24 raise DependencyNotInstalled(“box2D is not installed, run pip install gym[box2d]”) 27 if TYPE_CHECKING: 28 import pygame DependencyNotInstalled: box2D is not installed, run pip install gym[box2d]

And here the error I get when trying to install box2D:

pip3 install --no-cache-dir pygame

Requirement already satisfied: pygame in /Users/Toni/myenv/lib/python3.12/site-packages (2.5.2)
(myenv) Toni@Air-de-Lucia pygame-1.9.6 % pip3 install --no-cache-dir “gym[box2d]”
Collecting gym[box2d]
Downloading gym-0.26.2.tar.gz (721 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 721.7/721.7 kB 5.9 MB/s eta 0:00:00
Installing build dependencies … done
Getting requirements to build wheel … done
Installing backend dependencies … done
Preparing metadata (pyproject.toml) … done
Collecting numpy>=1.18.0 (from gym[box2d])
Downloading numpy-1.26.4-cp312-cp312-macosx_10_9_x86_64.whl.metadata (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.1/61.1 kB 66.5 MB/s eta 0:00:00
Collecting cloudpickle>=1.2.0 (from gym[box2d])
Downloading cloudpickle-3.0.0-py3-none-any.whl.metadata (7.0 kB)
Collecting gym-notices>=0.0.4 (from gym[box2d])
Downloading gym_notices-0.0.8-py3-none-any.whl.metadata (1.0 kB)
Collecting box2d-py==2.3.5 (from gym[box2d])
Downloading box2d-py-2.3.5.tar.gz (374 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 374.4/374.4 kB 13.1 MB/s eta 0:00:00
Installing build dependencies … done
Getting requirements to build wheel … done
Installing backend dependencies … done
Preparing metadata (pyproject.toml) … done
Collecting pygame==2.1.0 (from gym[box2d])
Downloading pygame-2.1.0.tar.gz (5.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.8/5.8 MB 9.2 MB/s eta 0:00:00
Installing build dependencies … done
Getting requirements to build wheel … done
Installing backend dependencies … done
Preparing metadata (pyproject.toml) … done
Collecting swig==4.* (from gym[box2d])
Downloading swig-4.2.1-py2.py3-none-macosx_10_9_x86_64.whl.metadata (3.6 kB)
Downloading swig-4.2.1-py2.py3-none-macosx_10_9_x86_64.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 5.3 MB/s eta 0:00:00
Downloading cloudpickle-3.0.0-py3-none-any.whl (20 kB)
Downloading gym_notices-0.0.8-py3-none-any.whl (3.0 kB)
Downloading numpy-1.26.4-cp312-cp312-macosx_10_9_x86_64.whl (20.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 20.3/20.3 MB 9.3 MB/s eta 0:00:00
Building wheels for collected packages: box2d-py, pygame, gym
Building wheel for box2d-py (pyproject.toml) … done
Created wheel for box2d-py: filename=box2d_py-2.3.5-cp312-cp312-macosx_10_9_universal2.whl size=930456 sha256=0f33611cb279229d544eb47647312d6e3de1bc1ffe033829186934f08c56bb90
Stored in directory: /private/var/folders/mc/j32rprwn2qz132bngfg7nqpc0000gp/T/pip-ephem-wheel-cache-9zd1w6ld/wheels/2a/e9/60/774da0bcd07f7dc7761a8590fa2d065e4069568e78dcdc3318
Building wheel for pygame (pyproject.toml) … error
error: subprocess-exited-with-error

× Building wheel for pygame (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [87 lines of output]
clang: warning: argument unused during compilation: ‘-L/usr/local/lib’ [-Wunused-command-line-argument]
In file included from src_c/imageext.c:29:
In file included from src_c/pygame.h:30:
In file included from src_c/_pygame.h:42:
In file included from /usr/local/include/SDL2/SDL.h:38:
In file included from /usr/local/include/SDL2/SDL_cpuinfo.h:111:
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/immintrin.h:14:2: error: “This header is only meant to be used on x86 and x64 architecture”
#error “This header is only meant to be used on x86 and x64 architecture”
^
In file included from src_c/imageext.c:29:
In file included from src_c/pygame.h:30:
In file included from src_c/_pygame.h:42:
In file included from /usr/local/include/SDL2/SDL.h:38:
In file included from /usr/local/include/SDL2/SDL_cpuinfo.h:111:
In file included from /Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/immintrin.h:17:
In file included from /Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/x86gprintrin.h:15:
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/hresetintrin.h:42:27: error: invalid input constraint ‘a’ in asm
asm (“hreset $0” :: “a”(__eax));
^
In file included from src_c/imageext.c:29:
In file included from src_c/pygame.h:30:
In file included from src_c/_pygame.h:42:
In file included from /usr/local/include/SDL2/SDL.h:38:
In file included from /usr/local/include/SDL2/SDL_cpuinfo.h:111:
In file included from /Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/immintrin.h:21:
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:14:2: error: “This header is only meant to be used on x86 and x64 architecture”
#error “This header is only meant to be used on x86 and x64 architecture”
^
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:54:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_vec_init_v2si(__i, 0);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:133:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_packsswb((__v4hi)__m1, (__v4hi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:163:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_packssdw((__v2si)__m1, (__v2si)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:193:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_packuswb((__v4hi)__m1, (__v4hi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:220:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_punpckhbw((__v8qi)__m1, (__v8qi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:243:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_punpckhwd((__v4hi)__m1, (__v4hi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:264:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_punpckhdq((__v2si)__m1, (__v2si)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:291:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_punpcklbw((__v8qi)__m1, (__v8qi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:314:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_punpcklwd((__v4hi)__m1, (__v4hi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:335:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_punpckldq((__v2si)__m1, (__v2si)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:356:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_paddb((__v8qi)__m1, (__v8qi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:377:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_paddw((__v4hi)__m1, (__v4hi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:398:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_paddd((__v2si)__m1, (__v2si)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:420:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_paddsb((__v8qi)__m1, (__v8qi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:443:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_paddsw((__v4hi)__m1, (__v4hi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/mmintrin.h:465:12: error: invalid conversion between vector type ‘__m64’ (vector of 1 ‘long long’ value) and integer type ‘int’ of different size
return (__m64)__builtin_ia32_paddusb((__v8qi)__m1, (__v8qi)__m2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

  ---
  For help with compilation see:
      https://www.pygame.org/wiki/MacCompile
  To contribute to pygame development see:
      https://www.pygame.org/contribute.html
  ---
  
  error: command '/usr/bin/clang' failed with exit code 1
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pygame
Building wheel for gym (pyproject.toml) … done
Created wheel for gym: filename=gym-0.26.2-py3-none-any.whl size=827618 sha256=215ec1465a5d0f217a5ccb6b10a38885e484ee8304aac5953c1c68de88842579
Stored in directory: /private/var/folders/mc/j32rprwn2qz132bngfg7nqpc0000gp/T/pip-ephem-wheel-cache-9zd1w6ld/wheels/95/51/6c/9bb05ebbe7c5cb8171dfaa3611f32622ca4658d53f31c79077
Successfully built box2d-py gym
Failed to build pygame
ERROR: Could not build wheels for pygame, which is required to install pyproject.toml-based projects

Cheers
Antonio

The “gym” package is obsolete and is no longer supported.

Hi @anlafuente

Although I really dont know if what Tom is stating is true, just in case if you still want to explore, I found two useful link which should help you resolve your issue.

The below GitHub mentions almost similar error as yours error, for which one of them has responded, try to go through all the comments.

Wish you all the best!!
Keep Learning!!!

Regards
DP

1 Like

See this:

From this repo:

2 Likes

Hi Tom @TMosh ,

There should be then another way for learners to be able to do the assignment locally??

Regards
DP

Also be aware, there are many backward compatibility issues with using “gymnasium” to replace “gym”.

One would hope so. I have not found it to be the case.

1 Like

Thanks Deepti,

For what it’s worth none of this seemed to work :frowning:

Would be nice to find a workaround though to be able to play and adapt this notebook locally
!

1 Like

And thanks to you too Tom :slight_smile:

1 Like

@anlafuente, you can try updating your version to use gymnasium instead of gym. You’ll need to make some changes as @TMosh mentioned, since there are backward compatibility issues, but I think it won’t be too bad. I ran through the first few sections (through section 5), and found these were the only changes I needed to make after pip installing gymnasium:

  • import gymnasium as gym
  • there’s an extra info parameter returned by env.step() and env.reset(), so you need to add an extra underscore (_) for this extra return parameter that you can just ignore, like this:
next_state, reward, done, _, _ = env.step(action)
...
current_state, _ = env.reset()

There may be more that you’ll find if you do try this out and go through the whole project, but at first glance, it seems not too bad. You can google to get documentation on the gymnasium api if needed.

If you do try it, and find other things that need changing, please post it here. I’m going to let the course developers know that they may want to update assignment at some point to use gymnasium since gym is no longer supported, and I’ll update them with anything else you find to make their job easier when they get around to it. Other learners who are trying to run in their own environments may find it useful, too.

3 Likes

Thanks Wendy! I’ll try once I get past a new problem :sweat_smile:. I’m getting a “No module named gymnasium” error despite having properly installed it with pip.

1 Like