forked from openai/spinningup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit contains a solution to the problems explained in openai#142. It adds two ways for people to use a custom environment with the Spinningup package: 1. People can add the code that initializes the custom gym environment in the 'env_config.py' file. 2. People can use the 'env_pkg' cmd line argument to specify which the package should be imported for the custom environment to work.
- Loading branch information
Showing
4 changed files
with
134 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Module used for adding custom environments. | ||
Example: | ||
This module allows you to add your own custom gym environments to the | ||
Spinningup package. These environments should inherit from the :class:`gym.env` | ||
class. See | ||
`this issue on the openai github <https://github.com/openai/gym/blob/master/docs/creating-environments.md>`_ | ||
for more information on how to create custom environments. | ||
.. code-block:: python | ||
import custom environment | ||
""" # noqa: E501 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Module that contains utilities that can be used with the | ||
`openai gym package <https://github.com/openai/gym>`_. | ||
""" | ||
|
||
import importlib | ||
import sys | ||
|
||
|
||
def import_gym_env_pkg(module_name, frail=True, dry_run=False): | ||
"""Tries to import the custom gym environment package. | ||
Args: | ||
module_name (str): The python module you want to import. | ||
frail (bool, optional): Throw ImportError when tensorflow can not be imported. | ||
Defaults to ``true``. | ||
dry_run (bool, optional): Do not actually import tensorflow if available. | ||
Defaults to ``False``. | ||
Raises: | ||
ImportError: A import error if the package could not be imported. | ||
Returns: | ||
union[tf, bool]: Custom env package if ``dry_run`` is set to ``False``. | ||
Returns a success bool if ``dry_run`` is set to ``True``. | ||
""" | ||
module_name = module_name[0] if isinstance(module_name, list) else module_name | ||
try: | ||
if module_name in sys.modules: | ||
if not dry_run: | ||
return sys.modules[module_name] | ||
else: | ||
return True | ||
elif importlib.util.find_spec(module_name) is not None: | ||
if not dry_run: | ||
return importlib.import_module(module_name) | ||
else: | ||
return True | ||
else: | ||
if frail: | ||
raise ImportError("No module named '{}'.".format(module_name)) | ||
return False | ||
except (ImportError, KeyError, AttributeError) as e: | ||
if ImportError: | ||
if not frail: | ||
return False | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters