Search code examples
pythonlinuxjupyter-notebookreinforcement-learninggoogle-colaboratory

Colaboratory: how to install PyGame Learning Environment


In Colaboratory, we are forced to use pip install. Some third packages such as gym, PLE, etc, their installation should be

git clone https://github.com/ntasfi/PyGame-Learning-Environment.git
cd PyGame-Learning-Environment/
pip install -e .

When I tried, here are some problems:

1) !cd doesn't work here. I'm still in my current folder in google drive.

2) Instead, I directly run:

!git clone https://github.com/ntasfi/PyGame-Learning-Environment.git 
!pip install -e PyGame-Learning-Environment 

And it says successfully installed, but I cannot import it. I checked and it doesnt appear in the /usr/local/lib/python3.6/dist-packages

I also checked the python by:

import os
print(os.getcwd())

which gives me: /content, a directory I dont understand. And obviously I cannot import the package.

What should I do?


Solution

  • Here's a worked example: https://colab.research.google.com/drive/1PsPArPkxnhCIKSFK2gDA46_vnLMc1U9z

    Tips:

    • Use os.chdir instead of !cd. ! commands execute in a subshell, so their effects won't apply to subsequent commands. os.chdir will, however.
    • You want to check sys.path instead of site-packages since you're installing with -e.
    • You'll also need to install pygame. It looks like that's an unlisted dep.

    Full install recipe:

    import os
    !git clone https://github.com/ntasfi/PyGame-Learning-Environment.git
    os.chdir('PyGame-Learning-Environment')
    !pip install -e .
    !pip install pygame
    os.chdir('/content')