Antoine Vo
Antoine Vo

Reputation: 571

OpenAI Gym - AttributeError: module 'contextlib' has no attribute 'nullcontext'

I'm running into this error when trying to run a command from docker a docker container on google compute engine.

Here's the stacktrace:

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

Upvotes: 9

Views: 10283

Answers (3)

wei liming
wei liming

Reputation: 11

You need a specific gym version (==0.15.7) to run contextlib, I also have this error, and resolved by set the specific pytest version to pytest==7.0.1

Traceback (most recent call last):
File "setup.py", line 24, in <module>
    tests_require=['pytest-mock', 'pytest-cov', 'pytest==7.1.2', 'mock', 'tomli==1.2.2', 'coverage==6.4']
  File "/opt/teamcity/work/dd131e9564caa7d5/venv/lib64/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib64/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib64/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest_runner-6.0.0-py3.6.egg/ptr/__init__.py", line 203, in run
    return self.run_tests()
  File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest_runner-6.0.0-py3.6.egg/ptr/__init__.py", line 214, in run_tests
    result_code = __import__('pytest').main()
  File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest-7.1.2-py3.6.egg/pytest/__init__.py", line 28, in <module>
    from _pytest.logging import LogCaptureFixture
  File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest-7.1.2-py3.6.egg/_pytest/logging.py", line 7, in <module>
    from contextlib import nullcontext
ImportError: cannot import name 'nullcontext'

Upvotes: 1

a piece of cheese
a piece of cheese

Reputation: 61

Degrading the version of gym may also solve this problem. I met this error with python 3.6, too. And I degraded gym's version from 0.21.0 to 0.15.7, then it was fixed.

Upvotes: 6

Antoine Vo
Antoine Vo

Reputation: 571

It seems like this is an issue with python 3.6 and gym. Upgrading my container to python 3.7 fixed the issue.

Upvotes: 9

Related Questions