Skip to content

Commit

Permalink
fix: avoid gym registration in installation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEimer committed Dec 11, 2023
1 parent c214c3c commit 2e62f68
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions carl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
import importlib.util as iutil
import warnings

from gymnasium.envs.registration import register

from carl import envs

name = "CARL"
package_name = "carl-bench"
author = __author__
Expand All @@ -27,51 +23,58 @@
"""
version = __version__

try:
from gymnasium.envs.registration import register

for e in envs.gymnasium.classic_control.__all__:
register(
id=f"carl/{e}-v0",
entry_point=f"carl.envs.gymnasium.classic_control:{e}",
)
from carl import envs

for e in envs.gymnasium.classic_control.__all__:
register(
id=f"carl/{e}-v0",
entry_point=f"carl.envs.gymnasium.classic_control:{e}",
)

def check_spec(spec_name: str) -> bool:
"""Check if the spec is installed
def check_spec(spec_name: str) -> bool:
"""Check if the spec is installed
Parameters
----------
spec_name : str
Name of package that is necessary for the environment suite.
Parameters
----------
spec_name : str
Name of package that is necessary for the environment suite.
Returns
-------
bool
Whether the spec was found.
"""
spec = iutil.find_spec(spec_name)
found = spec is not None
if not found:
with warnings.catch_warnings():
warnings.simplefilter("once")
warnings.warn(
f"""Module {spec_name} not found. If you want to use these environments,
please follow the installation guide."""
)
return found

Returns
-------
bool
Whether the spec was found.
"""
spec = iutil.find_spec(spec_name)
found = spec is not None
if not found:
with warnings.catch_warnings():
warnings.simplefilter("once")
warnings.warn(
f"""Module {spec_name} not found. If you want to use these environments,
please follow the installation guide."""
# Environment loading
found = check_spec("Box2D")
if found:
for e in envs.gymnasium.box2d.__all__:
register(
id=f"carl/{e}-v0",
entry_point=f"carl.envs.gymnasium.box2d:{e}",
)
return found


# Environment loading
found = check_spec("Box2D")
if found:
for e in envs.gymnasium.box2d.__all__:
found = check_spec("py4j")
if found:
register(
id=f"carl/{e}-v0",
entry_point=f"carl.envs.gymnasium.box2d:{e}",
id="carl/CARLMario-v0",
entry_point="carl.envs.gymnasium:CARLMarioEnv",
)

found = check_spec("py4j")
if found:
register(
id="carl/CARLMario-v0",
entry_point="carl.envs.gymnasium:CARLMarioEnv",
except:
print(
"""Gym registration failed - this is normal during installation.
After that, please check that gymnasium is installed correctly."""
)

0 comments on commit 2e62f68

Please sign in to comment.