On Windows, TensorFlow reports either or both of the following errors after executing an import tensorflow
statement:
No module named \"_pyw
tensorflow 1.3 does not support cuda 9.0 yet. I degrade to cuda 8.0, then it works.
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.
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
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).
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.
Ran into the same problem (in 2019/09/09) while investigating [SO]: Error while training using the estimator API in tensorflow.
Setup:
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:
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!!!