'How to copy gym environment?

Info: I am using OpenAI Gym to create RL environments but need multiple copies of an environment for something I am doing. I do not want to do anything like [gym.make(...) for i in range(2)] to make a new environment.

Question: Given one gym env what is the best way to make a copy of it so that you have 2 duplicate but disconnected envs?

Here is an example:

import gym

env = gym.make("CartPole-v0")
new_env = # NEED COPY OF ENV HERE

env.reset() # Should not alter new_env


Solution 1:[1]

You can use copy.deepcopy() to copy the current environment :

import gym
import copy

env = gym.make("CartPole-v0")
env.reset()

env_2 = copy.deepcopy(env)

env.step() # Stepping through `env` will not alter `env_2`

However note that this solution might not work in case of custom environment, if it contains things that can't be deepcopied (like generators).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1