OpenAI Gym – AttributeError: module ‘contextlib’ has no attribute ‘nullcontext’

I’m running into this error when trying to run training on my StableBaselines Gym Environment from a virtual machine on Google Compute Engine. I can’t seem to find the issue, I’ve tried upgrading to the most recent versions for all of my libraries but it didn’t seem to fix the issue. Does anyone have any idea what this could be? Thanks!

Here’s the stacktrace:

Traceback (most recent call last):
  File "", line 16, in <module>
    from stable_baselines.ppo1 import PPO1
  File "/home/selfplay/.local/lib/python3.6/site-packages/stable_baselines/", line 3, in <module>
    from stable_baselines.a2c import A2C
  File "/home/selfplay/.local/lib/python3.6/site-packages/stable_baselines/a2c/", line 1, in <module>
    from stable_baselines.a2c.a2c import A2C
  File "/home/selfplay/.local/lib/python3.6/site-packages/stable_baselines/a2c/", line 3, in <module>
    import gym
  File "/home/selfplay/.local/lib/python3.6/site-packages/gym/", line 13, in <module>
    from gym.envs import make, spec, register
  File "/home/selfplay/.local/lib/python3.6/site-packages/gym/envs/", line 10, in <module>
  File "/home/selfplay/.local/lib/python3.6/site-packages/gym/envs/", line 269, in load_env_plugins
    context = contextlib.nullcontext()
AttributeError: module 'contextlib' has no attribute 'nullcontext'

Source: Python Questions