How to create and use a custom OpenAI gym environment on google colab?
题意 :如何在Google Colab上创建和使用自定义的OpenAI Gym环境
问题背景:
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?
问题解决:
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.
我认为你只需要重启你的运行时环境。之后它应该就能正常工作了。我通常会在我的笔记本顶部加上以下内容。每当我修改环境代码以更新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.
你会收到一个警告,说环境意外地结束了,或者类似的信息,但没关系。现在笔记本已经下载并安装了仓库的最新版本。现在使用简单的import gym_foo应该可以工作了。

