Render Issue with Official Reinforcement Learning Tutorial

BaruchG: <p>Hi all,<br>
I’m having some trouble running the official reinforcement learning <a href="https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html#training" rel="nofollow noopener">tutorial</a> in the available <a href="https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/2b3f06b04b5e96e4772746c20fcb4dcc/reinforcement_q_learning.ipynb" rel="nofollow noopener">colab notebook</a>.  I haven’t done anything beyond try to run the cells but I keep getting an error from (I believe) gym’s render function. I don’t know if colab won’t run the render function for some reason or if I am just doing something wrong, but some clarity would be great!<br>
The code in the cell is:</p>
<pre><code class="lang-auto">resize = T.Compose([T.ToPILImage(),
                    T.Resize(40, interpolation=Image.CUBIC),
                    T.ToTensor()])


def get_cart_location(screen_width):
    world_width = env.x_threshold * 2
    scale = screen_width / world_width
    return int(env.state[0] * scale + screen_width / 2.0)  # MIDDLE OF CART

def get_screen():
    # Returned screen requested by gym is 400x600x3, but is sometimes larger
    # such as 800x1200x3. Transpose it into torch order (CHW).
    screen = env.render(mode='rgb_array').transpose((2, 0, 1))
    # Cart is in the lower half, so strip off the top and bottom of the screen
    _, screen_height, screen_width = screen.shape
    screen = screen[:, int(screen_height*0.4):int(screen_height * 0.8)]
    view_width = int(screen_width * 0.6)
    cart_location = get_cart_location(screen_width)
    if cart_location &lt; view_width // 2:
        slice_range = slice(view_width)
    elif cart_location &gt; (screen_width - view_width // 2):
        slice_range = slice(-view_width, None)
    else:
        slice_range = slice(cart_location - view_width // 2,
                            cart_location + view_width // 2)
    # Strip off the edges, so that we have a square image centered on a cart
    screen = screen[:, :, slice_range]
    # Convert to float, rescale, convert to torch tensor
    # (this doesn't require a copy)
    screen = np.ascontiguousarray(screen, dtype=np.float32) / 255
    screen = torch.from_numpy(screen)
    # Resize, and add a batch dimension (BCHW)
    return resize(screen).unsqueeze(0).to(device)


env.reset()
plt.figure()
plt.imshow(get_screen().cpu().squeeze(0).permute(1, 2, 0).numpy(),
           interpolation='none')
plt.title('Example extracted screen')
plt.show()
</code></pre>
<p>and the stack trace that I get is:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
&lt;ipython-input-10-6a36d3e0eceb&gt; in &lt;module&gt;()
     38 env.reset()
     39 plt.figure()
---&gt; 40 plt.imshow(get_screen().cpu().squeeze(0).permute(1, 2, 0).numpy(),
     41            interpolation='none')
     42 plt.title('Example extracted screen')

&lt;ipython-input-10-6a36d3e0eceb&gt; in get_screen()
     12     # Returned screen requested by gym is 400x600x3, but is sometimes larger
     13     # such as 800x1200x3. Transpose it into torch order (CHW).
---&gt; 14     screen = env.render(mode='rgb_array').transpose((2, 0, 1))
     15     print(screen)
     16     # Cart is in the lower half, so strip off the top and bottom of the screen

/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/cartpole.py in render(self, mode)
    172 
    173         if self.viewer is None:
--&gt; 174             from gym.envs.classic_control import rendering
    175             self.viewer = rendering.Viewer(screen_width, screen_height)
    176             l, r, t, b = -cartwidth / 2, cartwidth / 2, cartheight / 2, -cartheight / 2

/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/rendering.py in &lt;module&gt;()
     23 
     24 try:
---&gt; 25     from pyglet.gl import *
     26 except ImportError as e:
     27     raise ImportError('''

/usr/local/lib/python3.6/dist-packages/pyglet/gl/__init__.py in &lt;module&gt;()
    233 elif compat_platform == 'darwin':
    234     from .cocoa import CocoaConfig as Config
--&gt; 235 del base  # noqa: F821
    236 
    237 

NameError: name 'base' is not defined
&lt;Figure size 432x288 with 0 Axes&gt;
</code></pre>
<p>Thanks!</p>