Search code examples
pythonpackagegoogle-colaboratoryopenai-gym

How to create and use a custom OpenAI gym environment on google colab?


I have succeeded in creating a custom OpenAI gym environment on my computer following this tutorial: https://web.archive.org/web/20181128171840/https://medium.com/@apoddar573/making-your-own-custom-environment-in-gym-c3b65ff8cdaa

Then I've uploaded my package to colab as a zip file and installed it:

!unzip /content/gym-foo.zip
!pip install -e /content/gym-foo

After that I've tried using my custom environment:

import gym
import gym_foo
gym.make("gym_foo-v0")

This actually works on my computer, but on google colab it gives me:

ModuleNotFoundError: No module named 'gym_foo'

Whats going on? How can I use my custom environment on google colab?


Solution

  • I think you just need to restart your runtime. It should work after that. I usually have the following at the top of my notebook. I run the following, whenever I made a change in my environment code to update the version on colab.

    %%capture
    !rm -r Foo_env
    !git clone https://github.com/username/Foo_env.git
    !pip install -e Foo_env
    

    I then restart my runtime through running the following block.

    import os
    
    def restart_runtime():
      os.kill(os.getpid(), 9)
    
    restart_runtime()
    

    You'll get a warning that the environment quite unexpectedly, or something similar, but that's fine. Now the notebook has the newest version of my repo downloaded and installed. Using a simple import gym_foo should work now.