Search code examples
python-3.xdllinstallationtensorflow2.0miniconda

Installing Tensorflow 2 gets a dll failed to load in pywrap_tensorflow.py


I have had problems here, here and there installing TensorFlow 2 over the last year or so. So I am trying Miniconda.

I have an AMD Radeon hd 6670 and an AMD Radeon hd 6450.

I just downloaded Miniconda and made an environment and did a pip install --upgrade tensorflow in a Miniconda prompt on Windows 8.1 and got TensorFlow 2.2.

When I try to import tensorflow I get the stack trace below.

I did download Visual Studio to get the latest redistributebles (I think).

seems like this occurs near this line: from tensorflow.python.pywrap_tensorflow_internal import *

Edit 1: I used this yaml file for python 3.6 (the other was 3.7), but it produced the same error.

Edit 2: I upgraded to Conda 4.8.3 and Python 3.7 (in the yaml file) and got the same error. This is the line in pywrap internal that shows the problem:

line 28: _pywrap_tensorflow_internal = swig_import_helper()

In swig helper:

fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])

Looks like it's trying to find itself? Anyone have a clue?

Edit 3: This looks like the same problem, but it's dated. This seems to be the same problem that got fixed by using 2015 C++ redistributebles. I do have 2015 x64 and 2015-2019 x86 installed.

Edit 4: Trying 3.5 gets a subprocess error (please see below).

Edit 5: Trying 3.7 with: conda install 'tensorflow=*=mkl*' (and no yaml file) did work (3.6 did not work). Unfortunately it still takes 10 seconds to import tf2!

Searching for: -gpu swig_import_helper "Failed to load the native TensorFlow runtime" results in about 3.6k hits, so this is an ongoing problem.

Edit 5: installing Keras breaks with this bug. trying the fix gets: AttributeError: module 'tensorflow' has no attribute 'compat'. So TensorFlow does not work anymore.

Does anyone have a repeatable process that is stable? I am trying to work with the code from Deep Learning with Python, Second Edition.

Edit 6: unstalling minconda took about an hour. installing a new anaconda worked fine. using their yaml file gets a nice install. but importing tensorflow gets a dll load error.

Edit 7: trying Python 3.8 and using a virtual environment is much faster. I can install TensorFlow. But using it gets:

ImportError: Could not find the DLL(s) 'msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a directory that is named in your %PATH% environmen
t variable. 

I do have the redistributables installed and windows/system32 is on the path:

(handson-ml2) d:\ray\dev\mlbooks\handson-ml2>echo %PATH%
d:\pyvenvs\handson-ml2\Scripts;C:\Program Files (x86)\Common Files\Oracle\Java\j
avapath;c:\Rtools\bin;c:\Rtools\mingw_32\bin;C:\ProgramData\Oracle\Java\javapath
;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x8
6;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\Wi
ndowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Stati
c;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Program Files (x86)\AM
D\ATI.ACE\Core-Static;C:\gnuplot\bin;C:\Program Files\Git\cmd;C:\Program Files\C
alibre2\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files\MySQL\MySQ
L Utilities 1.6\;C:\Program Files (x86)\Common Files\Acronis\VirtualFile\;C:\Pro
gram Files (x86)\Common Files\Acronis\VirtualFile64\;C:\Program Files (x86)\Comm
on Files\Acronis\FileProtector\;C:\Program Files (x86)\Common Files\Acronis\File
Protector64\;C:\Program Files\nodejs\;C:\Program Files (x86)\Brackets\command;C:
\Program Files\Microsoft VS Code\bin;C:\Users\ray\AppData\Local\Programs\Python\
Launcher\;d:\bin;C:\Users\ray\AppData\Roaming\npm;C:\Users\ray\AppData\Local\ato
m\bin;C:\Program Files (x86)\FAHClient;x:\Program Files\Docker Toolbox

Here and here are a similar problems.

Some have suggested copying the dll's to the python path area.

Seems to have turned in to a warning overnight. Now I get:

(handson-ml2) d:\ray\dev\mlbooks\handson-ml2>py
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-06-25 13:53:13.746994: W tensorflow/stream_executor/platform/default/dso_lo
ader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64
_101.dll not found
2020-06-25 13:53:13.776619: 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.
Pip subprocess error:
ERROR: Could not find a version that satisfies the requirement bokeh>=2.0.0 (fro
m panel>=0.7.0->holoviews->-r D:\ray\dev\COVID-19\condaenv.tvdi0rp1.requirements
.txt (line 5)) (from versions: 0.2, 0.3, 0.4, 0.4.1, 0.4.4, 0.5.0, 0.5.1, 0.5.2,
 0.6.0, 0.6.1, 0.7.0, 0.7.1, 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.9.1, 0.9.2, 0.9.3, 0.
10.0, 0.11.0, 0.11.1, 0.12.0, 0.12.1, 0.12.2, 0.12.3, 0.12.4, 0.12.5, 0.12.6, 0.
12.7, 0.12.9, 0.12.10, 0.12.11, 0.12.13, 0.12.14, 0.12.15, 0.12.16, 0.13.0, 1.0.
0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.3.2, 1.3.4, 1.4.0)
ERROR: No matching distribution found for bokeh>=2.0.0 (from panel>=0.7.0->holov
iews->-r D:\ray\dev\COVID-19\condaenv.tvdi0rp1.requirements.txt (line 5))


CondaEnvException: Pip failed

This is the original stack trace:

Traceback (most recent call last):
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "d:\miniconda3\envs\tf2\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 "d:\miniconda3\envs\tf2\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "d:\miniconda3\envs\tf2\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 "2.py", line 1, in <module>
    import tensorflow as tf
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\__init__.py", line 50, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 69, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "d:\miniconda3\envs\tf2\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 "d:\miniconda3\envs\tf2\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "d:\miniconda3\envs\tf2\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.

Solution

  • Status Quo

    I ran into a comparable problem (this is the furthest i got) reproducibly on two machines. Some of the discussed issues seems to be known for example here: 1 2 3 4. Not only to reproduce 2, it makes sense to also start using virtual environments in order to test multiple tf versions. This can be achieved like this: (link for virtualenv on windows)

    $ pip install --user pipenv
    

    Several things can be approached according to my knowledge.

    Checking the versions and compatibility

    As also commented on your other question, it is very important to pay attention to respectively fitting versions. I also talked about this in that answer. In which way did you install your python version(s) and anaconda? Because both user and system mode options are present on Windows (which you are assumably working on), which can interfere. To find that out, you can type these commands into the cmd: where anaconda and where python. This should output a list of found places in your system. If something is missing, you can also check PATH to see weather it was added there.

    Having corresponding versions is important - refer to here or here 2 as a starting point. Using the prebuilt wheels from the first link in this paragraph makes things easier as you just have to select the correct version and install it. The second link discusses the different combinations of tensorflow and python versions that are possible among other things.

    Another hint that could help when having multiple different versions (of python for example) is to always make sure to use the correct one. Calling the correct pip to install the respective wheel can then look like this in the cmd:

    C:\Users\XXXXX\AppData\Local\Programs\Python\Python38\Scripts\pip.exe install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl
    

    whereas the next command would give an error on my system after returning pip 20.1.1 from C:\Users\XXXXX\.conda\envs\tf-cpu-AVX-env\lib\site-packages\pip (python 3.7) (note the python version):

    pip.exe -V
    pip.exe install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl
    

    For these examples, the official prebuilt wheel from google would be used. Refer to this site where some recent tensorflow packages are presented. Note that this example is the wheel with neither GPU nor AVX. Because calling like this is error-prone and somewhat cumbersome, it is recommended to use the virtual environments as hinted in the beginning or the following:

    Using (ana)conda for Virtual Environments

    The virtual environments as said above dont just make it more convenient, but also more tightly organized. A good tool to do so is (ana)conda. For the anaconda gui, you can create a new venv under "environments" and set your python version to for example 3.5. Different Anaconda environments in the GUI Then you can search for the tensorflow package and install it very easily via the package list next to the environment list. You may of course have to install other packages as well, e.g. Pandas, scikit-learn, numpy.

    For use of Conda with the command line, this link provides a good help for the first steps (also from your other question mentioned above).

    Further reading on this topic (was about downgrading tf): here

    Checking for unmet dependencies

    For troubleshooting the "DLL load failed" problem on Windows systems, dependencies can be checked with this DLL dependency analyzer tool This comes from the link 4 from the beginning, leading to this post explanation on how to approach DLL load failed problems on Windows. With that, it was discovered on my system that this dynamic library was missing: api-ms-win-core-wow64-11-1-0.dll. Unresolved dependency found with the tool named above

    Adressing Tensorflow CPU/GPU issues

    One of the possible sources for your trouble is incompatibilities with Tensorflow-GPU. This is because the default TF package contains both the CPU and GPU versions since the TF 2.1 release.

    To check if this causes some of your problems, a CPU-only variant can be tried first. You could for example try to install the correct tensorflow-CPU wheel from here (this is python 3.7 and tensorflow 2.0, decide weather to use AVX or not depending on the capabilities of your processor) or the google source named above.


    For Tensorflow-GPU, the following prerequisites have to be met:

    Installation of NVIDIA cuDNN (a GPU-accelerated library of primitives for deep neural networks) as e.g. cudnn-11.0-windows-x64-v8.0.1.13 here. After registration for the NVIDIA developer program, this can be accessed here.

    Please pay attention to the correct versions for a compatible CUDA Installation - see above! The current Tensorflow (2.2.0, but also true for TF ≥ 2.1.0 as of 23.07.2020) needs

    • CUDA 10.1 and the respective cuDNN ⤷ this is excactly matched only in the cuDNNv7.6.5 of November 5th, 2019, afaik.
    • For Cuda 10.1, a fitting NVIDIA® GPU driver of version 418.x or higher is required. This can be accessed e.g. here but has to be picked according to your platform. (Pay attention to not use a DCH driver where not applicable, see here.)
    • CUPTI, which ships with the CUDA Toolkit.
    • cuDNN SDK (>= 7.6)
    • (Optional) TensorRT 6.0 to improve latency and throughput for inference on some models.

    (This is partly taken from here)

    In order to install cuDNN (see above), the correct archive has to be downloaded, unzipped and copied from its directory the respective CUDA x.y install location. For windows, the locations for the three files are: (where x.y matches your specific installation as for example 10.2 - more information here and here)

    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vx.y\bin
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vx.y\include
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vx.y\lib
    

    Actually, not using the exact CUDA version 10.1 (but e.g. 10.2 instead) will cause errors such as: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found

    To test your tensorflow configuration on AVX or GPU availability, you can use this approach.

    Using Tensorflow on your GPU

    I noticed, that you are using two AMD GPUs. I'm not aware of a port with OpenGL or something like this, as it is always talked about the CUDA Compute Capability (reference - it should be ≥3.5 for tensorflow and is only applicable for NVIDIA GPUs)

    Please refer to this answer (or probably also others) for solutions to problems related to missing CUDA support.

    Further reading on enabling the GPU for Tensorflow: here

    If all of the above doesn't help, another possible approach would be to build tensorflow from source by yourself (which could also lead to increased performance).