无法运行导入tensorflow as tf的命令。

5
我使用以下命令安装了TensorFlow。
pip install tensorflow

我正在运行一个简单的程序,它是:

import tensorflow as tf
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)

我在运行程序时遇到了以下错误。
Traceback (most recent call last):
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
return importlib.import_module(mname)
File "C:\Program Files (x86)\Python\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
File "<frozen importlib._bootstrap>", line 577, in module_from_spec
File "<frozen importlib._bootstrap_external>", line 906, in create_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
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:\Program Files (x86)\Python\lib\site-
packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
return importlib.import_module('_pywrap_tensorflow_internal')
File "C:\Program Files (x86)\Python\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "first.py", line 1, in <module>
import tensorflow as tf
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import *
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\__init__.py", line 51, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 52, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
return importlib.import_module(mname)
File "C:\Program Files (x86)\Python\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
File "<frozen importlib._bootstrap>", line 577, in module_from_spec
File "<frozen importlib._bootstrap_external>", line 906, in create_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
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:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Program Files (x86)\Python\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
return importlib.import_module('_pywrap_tensorflow_internal')
File "C:\Program Files (x86)\Python\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow_internal'


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

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

我已经更新了pip并在Python 3.5上运行程序。


2个回答

3

这个错误信息通常意味着 TensorFlow 的其中一个依赖项在您的系统上找不到。由于您正在 Windows 上使用仅限于 CPU 版本的 TensorFlow,最可能缺少的 DLL 是 msvcp140.dll,它是运行使用 Microsoft Visual C++ 2015 编译的程序所必需的。这个 DLL 通常作为 Visual Studio 的一部分或像 Anaconda 这样的一些 Python 分布版在开发者机器上安装。然而,Python.org 的 Python for Windows 分发版并没有包含它。

您可以通过下载和安装 Microsoft Visual C++ 2015 Redistributable Update 3 在您的系统上安装 msvcp140.dll

对于其他遇到这个问题的人,请注意,这个脚本 可以帮助诊断在 Windows 上安装 TensorFlow 时常见的问题。


非常感谢您能回复这个问题。 https://stackoverflow.com/questions/44498771/multilayer-encoder-output-state-to-multilayer-decoder-in-seq2seq-model-tf-1-0谢谢。 - pseudo_teetotaler

0

我不确定问题出在哪里,可能和你一样对错误信息一无所知,但既然你正在使用Python 3.x,难道不应该通过以下方式安装TensorFlow:

pip3 install tensorflow

要查看是否正在使用pip3,请键入:

pip3 -V

如果您需要更多帮助,请访问maclinux的官方教程。
希望这可以帮到您。
dan4o

尝试通过pip3 install tensorflow进行安装,但问题尚未解决。 - Akshat
此外,要打印这些值,我认为您需要使用session.run()或类似的东西。请在TensorFlow安装说明的“验证安装”部分(底部)进行检查。希望能有所帮助。 - 01101010

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接