set_gym_backend¶
- torchrl.envs.set_gym_backend(backend)[source]¶
将 gym 后端设置为某个值。
- 参数:
backend (python 模块, 字符串 或 返回模块的可调用对象) – 要使用的 gym 后端。当您希望在加载时避免导入 gym 时,请使用字符串或可调用对象。
示例
>>> import gym >>> import gymnasium >>> with set_gym_backend("gym"): ... assert gym_backend() == gym >>> with set_gym_backend(lambda: gym): ... assert gym_backend() == gym >>> with set_gym_backend(gym): ... assert gym_backend() == gym >>> with set_gym_backend("gymnasium"): ... assert gym_backend() == gymnasium >>> with set_gym_backend(lambda: gymnasium): ... assert gym_backend() == gymnasium >>> with set_gym_backend(gymnasium): ... assert gym_backend() == gymnasium
此类也可以用作函数装饰器。
示例
>>> @set_gym_backend("gym") ... def fun(): ... gym = gym_backend() ... print(gym) >>> fun() <module 'gym' from '/path/to/env/site-packages/gym/__init__.py'> >>> @set_gym_backend("gymnasium") ... def fun(): ... gym = gym_backend() ... print(gym) >>> fun() <module 'gymnasium' from '/path/to/env/site-packages/gymnasium/__init__.py'>