2017-08-28 92 views
0

我試圖運行簡單的tensorflow的Hello World程序,但它不能在CMD運行,但相同的代碼工作在Python的IDLE我怎麼可以在CMD下運行它是什麼我輸入:無法在使用張量流的python中定義變量?

的Microsoft Windows [版本10.0.15063] (c)2017微軟公司。版權所有。

C:\Users\Manish>python 
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tensorflow as tf 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Users\Manish\tensorflow.py", line 2, in <module> 
    hello = tf.constant('Hello, TensorFlow!') 
AttributeError: module 'tensorflow' has no attribute 'constant' 
>>> hello = tf.constant('Hello Man') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'tf' is not defined 
>>> sess = tf.Session() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'tf' is not defined 
>>> print(sess.run(hello)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'sess' is not defined 
>>> 

更新: 刪除該文件後我面對新的錯誤

Microsoft Windows [Version 10.0.15063] 
(c) 2017 Microsoft Corporation. All rights reserved. 

C:\Users\Manish>activte tensorflow-gpu 
'activte' is not recognized as an internal or external command, 
operable program or batch file. 

C:\Users\Manish>activate tensorflow-gpu 

(tensorflow-gpu) C:\Users\Manish>python 
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tensorflow as tf 
Traceback (most recent call last): 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper 
    return importlib.import_module(mname) 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 978, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 961, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 648, in _load_unlocked 
    File "<frozen importlib._bootstrap>", line 560, in module_from_spec 
    File "<frozen importlib._bootstrap_external>", line 922, in create_module 
    File "<frozen importlib._bootstrap>", line 205, 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:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module> 
    from tensorflow.python.pywrap_tensorflow_internal import * 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module> 
    _pywrap_tensorflow_internal = swig_import_helper() 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper 
    return importlib.import_module('_pywrap_tensorflow_internal') 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\__init__.py", line 24, in <module> 
    from tensorflow.python import * 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module> 
    from tensorflow.python import pywrap_tensorflow 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 52, in <module> 
    raise ImportError(msg) 
ImportError: Traceback (most recent call last): 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper 
    return importlib.import_module(mname) 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 978, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 961, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 648, in _load_unlocked 
    File "<frozen importlib._bootstrap>", line 560, in module_from_spec 
    File "<frozen importlib._bootstrap_external>", line 922, in create_module 
    File "<frozen importlib._bootstrap>", line 205, 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:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module> 
    from tensorflow.python.pywrap_tensorflow_internal import * 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module> 
    _pywrap_tensorflow_internal = swig_import_helper() 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper 
    return importlib.import_module('_pywrap_tensorflow_internal') 
    File "C:\Users\Manish\Anaconda3\envs\tensorflow-gpu\lib\importlib\__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
ModuleNotFoundError: 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. 
+7

嘗試將您的'tensorflow.py'文件重命名爲其他內容。如果您的模塊與現有的已安裝模塊名稱相同,則可能會出現異常錯誤。 – Kevin

回答

0

Python有從其他模塊導入的符號的能力,而且也適用,如果這些模塊在同一個文件夾中。一個模塊可以只是一個.py文件,你可以用相同的語法從它導入符號。

現在你的問題是,你的腳本文件名爲tensorflow.py和陰影真正tensorflow模塊,所以任何import語句將不使用真正的tensorflow模塊,而是你自己的腳本。

解決方法是重命名腳本,並且不要使用您使用的模塊的名稱命名腳本。

+0

我已經刪除該文件,但我看到那些錯誤 –

+0

@darkstarmanny然後你有另一個同名的文件,或者tensorflow安裝不正確。如果你真的需要幫助,你應該提供更多細節。 –

+0

它在Python IDLE上工作正常,甚至在pycharm上,但不在cmd上 –