On Windows, running “import tensorflow” generates No module named “_pywrap_tensorflow” error

后端 未结 23 3278
误落风尘
误落风尘 2020-11-22 06:55

On Windows, TensorFlow reports either or both of the following errors after executing an import tensorflow statement:

  • No module named \"_pyw
相关标签:
23条回答
  • 2020-11-22 07:15

    tensorflow 1.3 does not support cuda 9.0 yet. I degrade to cuda 8.0, then it works.

    0 讨论(0)
  • 2020-11-22 07:16

    One may be tempted to keep the Powershell/cmd open on Windows. I've spent reasonable time till I decided to close and reopen my Powershell only to realize that I've done everything right.

    0 讨论(0)
  • 2020-11-22 07:17

    For Those Running on Older Hardware:

    You may get this same error due to having an older CPU using tensorflow-gpu 1.6.

    If your cpu was made before 2011, then your max tensorflow-gpu version is 1.5.

    Tensorflow 1.6 requires AVX instructions on your cpu. Verified here: Tensorflow Github docs

    AVX enabled CPUs: Wiki AVX CPUs

    What I did in my conda environment for tensorflow:

    pip install --ignore-installed --upgrade tensorflow-gpu==1.5
    
    0 讨论(0)
  • 2020-11-22 07:19

    In my case the "cudnn64_6.dll" file in the /bin folder had to be renamed to "cudnn64_5.dll" for the error to go away. I easily spent two hours to figure this out, and I followed the official install guide to the letter. This is true for installation via pip (officially supported) and conda (community supported).

    0 讨论(0)
  • 2020-11-22 07:19

    For tensorflow with CPU only:


    I had installed tensorflow using command:

    pip3 install --upgrade tensorflow
    

    This installed tensorflow 1.7
    But could not import the tensorflow from withing python 3.6.5 amd64 using:

    import tensorflow as tf
    

    So, i downgraded the tensorflow version from 1.7 to 1.5 using following command:

    pip3 install tensorflow==1.5
    

    This uninstalled the previous version and installed 1.5. Now it works.

    Seems that, my CPU does not support AVX instruction set that is needed in tensorflow 1.7

    I had MSVCP140.DLL in the system folders and .DLL in the PATHEXT variable in Environment Variable.

    0 讨论(0)
  • 2020-11-22 07:22

    Ran into the same problem (in 2019/09/09) while investigating [SO]: Error while training using the estimator API in tensorflow.

    Setup:

    • Win 10 x64
    • Python 3.7.3 (x64)
    • TensorFlow-GPU 1.13.1 ([TensorFlow]: Install TensorFlow with pip)

    Error:

    [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057588589]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" -c "import tensorflow as tf"
    Traceback (most recent call last):
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
        from tensorflow.python.pywrap_tensorflow_internal import *
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
        _pywrap_tensorflow_internal = swig_import_helper()
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\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 "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\imp.py", line 242, in load_module
        return load_dynamic(name, filename, file)
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\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 "<string>", line 1, in <module>
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
        from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
        from tensorflow.python import pywrap_tensorflow
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
        raise ImportError(msg)
    ImportError: Traceback (most recent call last):
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
        from tensorflow.python.pywrap_tensorflow_internal import *
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
        _pywrap_tensorflow_internal = swig_import_helper()
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\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 "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\imp.py", line 242, in load_module
        return load_dynamic(name, filename, file)
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\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.
    

    Looking at the "faulty" module (thanks to Dependency Walker), it turns out that it's not itself that's missing, but some of its dependencies (the cu*_100.dll files).

    Check [SO]: Python Ctypes - loading dll throws OSError: [WinError 193] %1 is not a valid Win32 application (@CristiFati's answer) (the Conclusions section at the end) for more details on this kind of errors.
    I had an older CUDA Toolkit version (8), and as a consequence, the cu*_80.dll files.

    Upgrading to TensorFlow-GPU 1.14.0 ("e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" -m pip install --upgrade tensorflow-gpu), made the error a bit clearer (and also shorter):

    [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057588589]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" -c "import tensorflow as tf"
    Traceback (most recent call last):
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\platform\self_check.py", line 75, in preload_check
        ctypes.WinDLL(build_info.cudart_dll_name)
      File "c:\install\x64\python\python\03.07.03\Lib\ctypes\__init__.py", line 356, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: [WinError 126] The specified module could not be found
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\__init__.py", line 28, in <module>
        from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
        from tensorflow.python import pywrap_tensorflow
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 30, in <module>
        self_check.preload_check()
      File "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\lib\site-packages\tensorflow\python\platform\self_check.py", line 82, in preload_check
        % (build_info.cudart_dll_name, build_info.cuda_version_number))
    ImportError: Could not find 'cudart64_100.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 10.0 from this URL: https://developer.nvidia.com/cuda-90-download-archive
    

    Steps:

    • Uninstall any CUDA Toolkit version (optional)
    • Install [nVidia.Developer]: CUDA Toolkit 10.0 Archive
      • Make sure to install v10.0 (that this TensorFlow-GPU version was built against). I installed v10.1 (which was the latest, and also the recommended version at the answer time), and the .dll names didn't match (cu*_101.dll). Since I didn't want to install v10.0, I created some symlinks (with the "correct" names) to the existing files, and it worked. But bear in mind that this is unsupported!!! You may experience funny behavior (including crashes). This is a (lame) workaround (gainarie)
      • Additionally, a compatible (meaning that it's for a specific CUDA Toolkit version) cuDNN version ([nVidia.Developer]: cuDNN Archive) is required. In order to access the download URL, nVidia membership is required

    After the above steps, and also setting the correct paths, it worked:

    [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057588589]> set PATH=%PATH%;%CUDA_PATH%\bin;f:\Install\x64\NVidia\GPU Computing Toolkit\cuDNN\7.6\bin
    
    [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057588589]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" -c "import tensorflow;print(\"Success!!!\")"
    Success!!!
    
    0 讨论(0)
提交回复
热议问题