I’m having trouble with my Python kernel crashing after running OpenAI Gym simulations in Jupyter notebook.
I installed the gym library using pip install gym and verified the installation was successful. When I run a basic training loop, everything works fine during execution:
import gym
# Create environment
environment = gym.make('CartPole-v1')
# Run training episodes
for episode in range(10):
state = environment.reset()
for step in range(200):
environment.render()
print(state)
move = environment.action_space.sample()
state, score, finished, data = environment.step(move)
if finished:
print(f"Training completed after {step+1} steps")
break
The simulation window opens correctly and shows the expected behavior. However, once all episodes finish running, my kernel dies and automatically restarts. I’ve tried using environment.close() after the loop, but this destroys the environment object completely. Has anyone encountered this issue before? What’s the proper way to clean up gym environments without killing the kernel?