简体   繁体   中英

Trouble with import TensorFlow in Jupyter Notebook while runs fine in bash

I installed tensor flow with "Installing with native pip" without installing anaconda and am able to import TensorFlow in Python Console launched from Terminal. image

$ python
Python 2.7.13 (default, Apr  4 2017, 08:47:57)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-04-30 20:54:36.278740: W     tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.278773: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.278785: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.278794: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.423246: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:865] OS X does not support NUMA - returning NUMA node zero
2017-04-30 20:54:36.432456: I     tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 with     properties:
name: GeForce GTX 1080
major: 6 minor: 1 memoryClockRate (GHz) 1.86
pciBusID 0000:01:00.0
Total memory: 8.00GiB
Free memory: 6.12GiB
2017-04-30 20:54:36.432507: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0
2017-04-30 20:54:36.432511: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y
2017-04-30 20:54:36.432519: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0)
>>> print(sess.run(hello))
Hello, TensorFlow!
>>>

But I run the same code in Jupiter notebook and it showed. image

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-d7933b52e0de> in <module>()
----> 1 import tensorflow as tf
      2 hello = tf.constant('Hello, TensorFlow!')
      3 sess = tf.Session()
      4 print(sess.run(hello))

ImportError: No module named tensorflow

How can I tackle this problem ?

Python, Tensorflow & Jupyter either must be on save version python2 / python3. Below are the commands to setup jupyter & Tensorflow with python3

sudo apt install python3-pip
pip3 install --upgrade pip   ##optional
pip3 install jupyter


##Install Tensorflow
pip3 install --upgrade tensorflow

Test
$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>

##Launch jupyter
jupyter notebook   
(In Ubuntu you may need to launch ~/.local/bin/jupyter-notebook until you relaunch the bash i.e. PATH issue)
##Create new notepad and run
import tensorflow as tf  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM