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

Cannot import pybullet_envs #4368

Open
ManosL opened this issue Nov 4, 2022 · 7 comments
Open

Cannot import pybullet_envs #4368

ManosL opened this issue Nov 4, 2022 · 7 comments

Comments

@ManosL
Copy link

ManosL commented Nov 4, 2022

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

zzf-zzf commented Dec 5, 2022

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

@araffin
Copy link
Contributor

araffin commented Dec 22, 2022

You can patch gym as done in the RL Zoo: DLR-RM/rl-baselines3-zoo#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
Copy link

LJoson commented Dec 27, 2022

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

SiggyF commented Dec 30, 2022

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

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

araffin commented Feb 27, 2023

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

araffin commented Nov 8, 2023

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)

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

6 participants