Ml-agents: [Solved] Installing mlagents doesn't generate a compatible Tensorflow.

Created on 28 May 2020  路  7Comments  路  Source: Unity-Technologies/ml-agents

mlagents-learn generates a stack trace because of Tensorflow with any command

ImportError: DLL load failed: The specified module could not be found.

a requirements.txt file would be helpful to lock down the various dependencies.
Tensorflow messageboards suggest downgrading protobuff versions and tensorflow versions. Could someone freeze a recent version of their environment and post it? This might help find a combination of dependencies that work together with the latest release of mlagents.

To Reproduce

C:\Projects\Unity\ml-agents>python --version
Python 3.7.7
C:\Projects\Unity\ml-agents>pip list
Package    Version
---------- -------
pip        20.1.1
setuptools 41.2.0
C:\Projects\Unity\ml-agents>pip install mlagents --no-cache-dir

C:\Projects\Unity\ml-agents>pip list
Package                Version
---------------------- -----------
absl-py                0.9.0
astunparse             1.6.3
cachetools             4.1.0
certifi                2020.4.5.1
chardet                3.0.4
cloudpickle            1.4.1
gast                   0.3.3
google-auth            1.15.0
google-auth-oauthlib   0.4.1
google-pasta           0.2.0
grpcio                 1.29.0
h5py                   2.10.0
idna                   2.9
importlib-metadata     1.6.0
Keras-Preprocessing    1.1.2
Markdown               3.2.2
mlagents               0.16.1
mlagents-envs          0.16.1
numpy                  1.18.4
oauthlib               3.1.0
opt-einsum             3.2.1
Pillow                 7.1.2
pip                    20.1.1
protobuf               3.12.2
pyasn1                 0.4.8
pyasn1-modules         0.2.8
pypiwin32              223
pywin32                227
PyYAML                 5.3.1
requests               2.23.0
requests-oauthlib      1.3.0
rsa                    4.0
scipy                  1.4.1
setuptools             41.2.0
six                    1.15.0
tensorboard            2.2.1
tensorboard-plugin-wit 1.6.0.post3
tensorflow             2.2.0
tensorflow-estimator   2.2.0
termcolor              1.1.0
urllib3                1.25.9
Werkzeug               1.0.1
wheel                  0.34.2
wrapt                  1.12.1
zipp                   3.1.0
C:\Projects\Unity\ml-agents>mlagents-learn
Traceback (most recent call last):
  File "c:\python\python37\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "c:\python\python37\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "c:\python\python37\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "c:\python\python37\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "c:\python\python37\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\python\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python\Python37\Scripts\mlagents-learn.exe\__main__.py", line 4, in <module>
  File "c:\python\python37\lib\site-packages\mlagents\trainers\learn.py", line 12, in <module>
    from mlagents import tf_utils
  File "c:\python\python37\lib\site-packages\mlagents\tf_utils\__init__.py", line 1, in <module>
    from mlagents.tf_utils.tf import tf as tf  # noqa
  File "c:\python\python37\lib\site-packages\mlagents\tf_utils\tf.py", line 3, in <module>
    import tensorflow as tf  # noqa I201
  File "c:\python\python37\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "c:\python\python37\lib\site-packages\tensorflow\python\__init__.py", line 50, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "c:\python\python37\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 69, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "c:\python\python37\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "c:\python\python37\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "c:\python\python37\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "c:\python\python37\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "c:\python\python37\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

Environment:

  • Unity Version: 2018.4.23f1
  • OS + version: Microsoft Windows 10 Home
  • _ML-Agents version_: release_2_branch
  • _TensorFlow version_: 2.2.0
  • _Environment_: No environment, root directory running mlagents-learn --help`
bug

All 7 comments

I have also tried with Anaconda.

> python --version
Python 3.7.7
> pip list (before install)

Package      Version
------------ -------------------
certifi      2020.4.5.1
pip          20.0.2
setuptools   46.4.0.post20200518
wheel        0.34.2
wincertstore 0.2
> git status
On branch release_2_branch
Your branch is up to date with 'origin/release_2_branch'.
> unity version
2018.4.23f1
> ML Agents (com.unity.ml-agents)
1.0.2 preview
> pip install mlagents

(Python37MLAgents) C:\Projects\Unity\ml-agents>pip list
Package                Version
---------------------- -------------------
absl-py                0.9.0
astunparse             1.6.3
cachetools             4.1.0
certifi                2020.4.5.1
chardet                3.0.4
cloudpickle            1.4.1
gast                   0.3.3
google-auth            1.15.0
google-auth-oauthlib   0.4.1
google-pasta           0.2.0
grpcio                 1.29.0
h5py                   2.10.0
idna                   2.9
importlib-metadata     1.6.0
Keras-Preprocessing    1.1.2
Markdown               3.2.2
mlagents               0.16.1
mlagents-envs          0.16.1
numpy                  1.18.4
oauthlib               3.1.0
opt-einsum             3.2.1
Pillow                 7.1.2
pip                    20.0.2
protobuf               3.12.2
pyasn1                 0.4.8
pyasn1-modules         0.2.8
pypiwin32              223
pywin32                227
PyYAML                 5.3.1
requests               2.23.0
requests-oauthlib      1.3.0
rsa                    4.0
scipy                  1.4.1
setuptools             46.4.0.post20200518
six                    1.15.0
tensorboard            2.2.1
tensorboard-plugin-wit 1.6.0.post3
tensorflow             2.2.0
tensorflow-estimator   2.2.0
termcolor              1.1.0
urllib3                1.25.9
Werkzeug               1.0.1
wheel                  0.34.2
wincertstore           0.2
wrapt                  1.12.1
zipp                   3.1.0
(Python37MLAgents) C:\Projects\Unity\ml-agents>mlagents-learn --help
Traceback (most recent call last):
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "c:\python\anaconda3\envs\python37mlagents\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "c:\python\anaconda3\envs\python37mlagents\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\python\anaconda3\envs\python37mlagents\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\python\anaconda3\envs\python37mlagents\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\python\anaconda3\envs\Python37MLAgents\Scripts\mlagents-learn.exe\__main__.py", line 4, in <module>
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\mlagents\trainers\learn.py", line 12, in <module>
    from mlagents import tf_utils
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\mlagents\tf_utils\__init__.py", line 1, in <module>
    from mlagents.tf_utils.tf import tf as tf  # noqa
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\mlagents\tf_utils\tf.py", line 3, in <module>
    import tensorflow as tf  # noqa I201
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\__init__.py", line 50, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 69, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "c:\python\anaconda3\envs\python37mlagents\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "c:\python\anaconda3\envs\python37mlagents\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "c:\python\anaconda3\envs\python37mlagents\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

@jdunwoody This appears to be a general tensorflow problem, and not specific to ML-Agents.

Have you tried this? https://github.com/tensorflow/tensorflow/issues/22794#issuecomment-573297027 (which I found by following the link to https://www.tensorflow.org/install/errors)

@jdunwoody This appears to be a general tensorflow problem, and not specific to ML-Agents.

Have you tried this? tensorflow/tensorflow#22794 (comment) (which I found by following the link to https://www.tensorflow.org/install/errors)

That was exactly it! Thankyou very much!
Here is the solution in case the tensorflow link breaks:

For this problem

ImportError: DLL load failed: The specified module could not be found.

Install this
https://support.microsoft.com/en-my/help/2977003/the-latest-supported-visual-c-downloads

I also looked into the Tensorflow messageboards for reasons for this error but didn't get to the bottom of the discussion - to find this gem! Too many suggestions from the Tensorflow community suggesting various combinations of protobuf, tensorflow and cuda that I couldn't find a combination that works.

Would it be worthwhile adding this dependency to the mlagents installation docs, given that this requirement is now default for Linux and Windows Tensforflow?

I can raise a pull request if you'd like me to do it.

Glad that fixed it!

I think we should definitely add it to the FAQ here: https://github.com/Unity-Technologies/ml-agents/blob/master/docs/FAQ.md#tensorflow-dependency

An additional option would be to do a try-catch around the tensorflow import here https://github.com/Unity-Technologies/ml-agents/blob/801c05bacc73ff03da2265340c88e6c2b8f3a5df/ml-agents/mlagents/tf_utils/tf.py#L3
and log a better error message if the exception contains "DLL load failed". I like this option since it keeps the solution close to the problem, unfortunately I'm not on Windows so it's hard for me to test.

As for the installation instructions, @mmattar (who's currently on PTO until the end of the week) has some opinions on what should and shouldn't go in the install guide, and recently rewrote some large swaths of it.

If you want to make a pull request for either the FAQ or the try-catch, please do. You're welcome to for the installation instructions too, it just might take a few days to get it reviewed.

I'll submit a PR for at least the FAQ. I'll see how the guard code could work.
Thanks again for your response and suggestion.

Could this be made more obvious in the Installation Guide? I think it should be mentioned there pretty clearly, since I only found out the solution to this problem by searching through previous issues and finding this. I didn't even consider looking for a FAQ since I interpreted Tensorflow's problem as a problem or bug rather than a question.

Actually my bad, repairing the C++ redistributables did not solve my problem. I still get the following:

Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
2020-09-04 01:08:21.885272: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-09-04 01:08:21.888741: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

(not sure why it says win32, I'm definitely 64-bit)

EDIT: Needed to restart after CUDA install, ok good now ^_^

Was this page helpful?
0 / 5 - 0 ratings