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

Fix mujoco_py only installation #3072

Merged
merged 1 commit into from
Sep 12, 2022
Merged

Conversation

YouJiacheng
Copy link
Contributor

Only import mujoco_rendering when mujoco installed. Otherwise the try import code in gym.envs.mujoco.mujoco_env is useless.

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Screenshots

Please attach before and after screenshots of the change if applicable.

Checklist:

  • I have run the pre-commit checks with pre-commit run --all-files (see CONTRIBUTING.md instructions to set it up)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@YouJiacheng
Copy link
Contributor Author

YouJiacheng commented Sep 7, 2022

It is tricky that the test expect an ImportError. But if there is an ImportError, then

if MUJOCO_NOT_INSTALLED:
    raise error.DependencyNotInstalled(
        f"{MUJOCO_IMPORT_ERROR}. (HINT: you need to install mujoco)"
    )

will be useless.
I'd like to split the file, and raise error.DependencyNotInstalled in the file. Then also catch DependencyNotInstalled in the tests.

Update: Actually we don't need raise this error during import because make will trigger __init__, current implementation already raise this error in __init__. Just catch DependencyNotInstalled in the test is enough!

@pseudo-rnd-thoughts
Copy link
Contributor

What test do you mean? If you can solve a way that mujoco_py works without mujoco and mujoco works without mujoco_py then could you do that

@YouJiacheng
Copy link
Contributor Author

YouJiacheng commented Sep 7, 2022

all test import test.env.utils

gym/tests/envs/utils.py

Lines 11 to 31 in 824507b

def try_make_env(env_spec: EnvSpec) -> Optional[gym.Env]:
"""Tries to make the environment showing if it is possible.
Warning the environments have no wrappers, including time limit and order enforcing.
"""
# To avoid issues with registered environments during testing, we check that the spec entry points are from gym.envs.
if "gym.envs." in env_spec.entry_point:
try:
return env_spec.make(disable_env_checker=True).unwrapped
except ImportError as e:
logger.warn(f"Not testing {env_spec.id} due to error: {e}")
return None
# Tries to make all environment to test with
all_testing_initialised_envs: List[Optional[gym.Env]] = [
try_make_env(env_spec) for env_spec in gym.envs.registry.values()
]
all_testing_initialised_envs: List[gym.Env] = [
env for env in all_testing_initialised_envs if env is not None
]

@pseudo-rnd-thoughts
Copy link
Contributor

Oh, could you just add a DeprecatedError to the try except. That is expected to fail for python 3.6

@pseudo-rnd-thoughts
Copy link
Contributor

@YouJiacheng could you fix the PR?

@YouJiacheng
Copy link
Contributor Author

YouJiacheng commented Sep 12, 2022

I have found a way to fix it: Catch both ImportError and error.DependencyNotInstalled in test.env.utils.
But I also want to remove from gym.envs.mujoco.mujoco_rendering import RenderContextOffscreen, Viewer from __init__.py to simplify the code!

@pseudo-rnd-thoughts
Copy link
Contributor

For the catching both errors, see https://stackoverflow.com/questions/6470428/catch-multiple-exceptions-in-one-line-except-block

If it is necessary to remove that, then it will be a necessary breaking change

Copy link
Contributor

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YouJiacheng This looks good, could you fix the pre-commit issues

@YouJiacheng
Copy link
Contributor Author

YouJiacheng commented Sep 12, 2022

  • Thanks, I know how to catch both error in one line.
  • Not really necessary.
  • My PC have some problems with pre-commit environment install, I'm trying to fix it! (Finally I run black manually...)

pre-commit/pre-commit#2402
https://superuser.com/questions/1493887/what-is-the-default-registry-value-for-command-processors-autorun

Only import `mujoco_rendering` when `mujoco` installed.
Otherwise the `try import` code in `gym.envs.mujoco.mujoco_env` is useless.
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

Successfully merging this pull request may close these issues.

3 participants