August 19, 2019

Environment Error in OpenAI gym[atari] with Pycharm


Win64 + Pycharm + Python 3.7 + OpenAI gym


1. Install problem

When I run OpenAI Atari in Pycharm, I first install Atari as below:

pip install 'gym[atari]'

but get an error like:

Invalid requirement: ''gym[atari]''
Traceback (most recent call last):
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pac
kaging\", line 93, in __init__
    req = REQUIREMENT.parseString(requirement_string)
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 1632, in parseString
    raise exc
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 1622, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 3395, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 3717, in parseImpl
    return self.expr._parse( instring, loc, doActions, callPreParse=False )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 3378, in parseImpl
    loc, resultlist = self.exprs[0]._parse( instring, loc, doActions, callPreParse=False )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 1383, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Users\chenj\PycharmProjects\maze\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\pyp", line 2689, in parseImpl
    raise ParseException(instring, loc, self.errmsg, self)
pip._vendor.pyparsing.ParseException: Expected W:(abcd...) (at char 0), (line:1, col:1)

This error is easy to fix by changing the apostrophe to double quotes because Pycharm cannot parse it:

pip install "gym[atari]"

2. Environment configuration problem

I try to run the sample code:

import gym
env = gym.make("MsPacman-v0")
observation = env.reset()
for _ in range(1000):
    action = env.action_space.sample() # your agent here (this takes random actions)
    observation, reward, done, info = env.step(action)

    if done:
        observation = env.reset()

and get error like:

Traceback (most recent call last):
File "C:/Users/danie/Downloads/Programming/Python/Programming/Pycharm/", line 40, in 
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\envs\", line 156, in make
return registry.make(id, **kwargs)
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\envs\", line 101, in make
env = spec.make(**kwargs)
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\envs\", line 72, in make
cls = load(self.entry_point)
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\envs\", line 17, in load
mod = importlib.import_module(mod_name)
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in find_and_load_unlocked
File "", line 677, in load_unlocked
File "", line 728, in exec_module
File "", line 219, in call_with_frames_removed
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\envs\", line 1, in 
from gym.envs.atari.atari_env import AtariEnv
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\envs\atari\", line 9, in 
import atari_py
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\", line 1, in 
from .ale_python_interface import *
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\atari_py\", line 18, in 
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\", line 434, in LoadLibrary
return self.dlltype(name)
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\", line 356, in init
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

I search the stackOverflow and figure out it is a DLL's problem. The answer given by the author cannot solve my problem. As he says,

If you have ale_c.dll (most likely you are on Windows), refer to this answer to see how DLL's loaded with ctypes are basically LoadLibrary calls as I mentioned. It mentions all the stuff about DLL loading order etc. but, if you need a quick way around this, copy your dll under system32 folder.

but due to dependent library of ale_c.dll, you need to download additional DLL file to system32 folder. You can use the software Dependencies to check out which library you are missing.

Here is not the solution to this error, but it may be helpful for you.

When you miss any DLL file, you can go to DLL-FILES.COM to search for what you need, and then download it to system32 folder. This is not the solution because when you download the correct DLL file you can find and run module ale_c.dll, and after that Pycharm will tell you that the ale_c.dll is a win32 but not win64 library. There will be still an Import Error if you run your code.

Because all my DLL files are win64 and the Python 3.7 is win64 as well. I guess the atari_py downloaded by Pycharm is win32, so I delete it in my virtual environment and get an win64 Atari whl file. Install the whl file by:

pip install atari_py-1.2.1-cp37-cp37m-win_amd64.whl

and my MsPacman can work.


A lot thanks to my girlfriend SakuragawaAsaba. Love her!