bulletphysics / bullet3

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
http://bulletphysics.org
Other
12.21k stars 2.85k forks source link

Cannot import pybullet_envs #4368

Open ManosL opened 1 year ago

ManosL commented 1 year ago

Before starting I want to mention that I have install the OpenAI gym version 0.26.2 and pybullet version 3.2.5. When I try to import pybullet_envs package, I get the following error:

Traceback (most recent call last):
  File "/home/manosl/Desktop/MSc_Courses/MSc_Thesis/Code/soft_actor_critic_implementation/main.py", line 1, in <module>
    import pybullet_envs
  File "/home/manosl/Desktop/MSc_Courses/MSc_Thesis/Code/thesis_env/lib/python3.10/site-packages/pybullet_envs/__init__.py", line 15, in <module>
    register(
  File "/home/manosl/Desktop/MSc_Courses/MSc_Thesis/Code/thesis_env/lib/python3.10/site-packages/pybullet_envs/__init__.py", line 7, in register
    if id in registry.env_specs:
AttributeError: 'dict' object has no attribute 'env_specs'

Does anyone knows how to fix this issue? I suspect the reason for this is the following:

The error is raised at pybullet_envs/init.py which does the following:

def register(id, *args, **kvargs):
  if id in registry.env_specs:
    return
  else:
    return gym.envs.registration.register(id, *args, **kvargs)

I changed it to this and the error was removed

def register(id, *args, **kvargs):
  if id in registry:
    return
  else:
    return gym.envs.registration.register(id, *args, **kvargs)

If you want you can have a look Thanks

zzf-zzf commented 1 year ago

This resolution is not work for me, do you have any other method?

araffin commented 1 year ago

You can patch gym as done in the RL Zoo: https://github.com/DLR-RM/rl-baselines3-zoo/pull/256

The file: https://github.com/DLR-RM/rl-baselines3-zoo/blob/feat/gym-0.24/rl_zoo3/gym_patches.py and then use apply_api_compatibility=True when creating the env.

(soon you will be able to apply the patches by simply doing import rl_zoo3.gym_patches)

LJoson commented 1 year ago

This resolution is not work for me, do you have any other method?

when i change gym0.26.0 -> 0.18.0,it works

SiggyF commented 1 year ago

This error is caused by this update to the gym environment (since 0.26), the env_spec was removed in favour of a simple dictionary. The suggested change looks good to me. I used the following temporary workaround to keep using 0.26:

from collections import UserDict

import gym
import gym.envs.registration

# Do this before importing pybullet_envs (adds an extra property env_specs as a property to the registry, so it looks like the <0.26 envspec version)
registry = UserDict(gym.envs.registration.registry)
registry.env_specs = gym.envs.registration.registry
gym.envs.registration.registry = registry

import pybullet_envs
racecar_env = gym.make('RacecarBulletEnv-v0')
erwincoumans commented 1 year ago

This requires a fix in Bullet? Is there a run-time version check of gym, so we can implement both old and new method?

araffin commented 1 year ago

This requires a fix in Bullet? Is there a run-time version check of gym, so we can implement both old and new method?

This PR should already fix it: https://github.com/bulletphysics/bullet3/pull/4332 (it should work with both gym 0.21 and 0.26)

The check in gym is here: https://github.com/openai/gym/blob/master/gym/envs/registration.py#L497-L498

araffin commented 8 months ago

Fyi, I pushed and released on pypi a subset of pybullet envs compatible with gymnasium: https://github.com/araffin/pybullet_envs_gymnasium pip install pybullet_envs_gymnasium (I will crosspost this message as it may interest several people)