jupyter で ModuleNotFoundError: No module named 'tensorflow' エラー

What?

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data.read_data_sets("MNIST_data/", one_hot=True)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-3a141fdd5d37> in <module>()
----> 1 from tensorflow.examples.tutorials.mnist import input_data
      2 mnist = input_data.read_data.read_data_sets("MNIST_data/", one_hot=True)

ModuleNotFoundError: No module named 'tensorflow'

How to resolve?

  • pip で tensorflow をもう一度インストー
$ pip install tensorflow

Why?

  • 失敗したパッケージ
    • tensorflow-1.1.0-py3-none-any.whl
    • appdirs-1.4.3 numpy-1.12.1 packaging-16.8 protobuf-3.3.0 pyparsing-2.2.0 setuptools-35.0.2 six-1.10.0 tensorflow-1.1.0 werkzeug-0.12.1 wheel-0.29.0
  • 成功したパッケージ
    • tensorflow-1.1.0-cp36-cp36m-macosx_10_11_x86_64.whl
    • numpy-1.12.1 protobuf-3.3.0 tensorflow-1.1.0 werkzeug-0.12.1
  • OS X 用のパッケージになっていなかったぽい(チュートリアルに沿ってるんだけどな。。。)

問題の切り分けで試したこと

  • jupyter notebook を立ち上げず、コンソールで直接試す
    • コンソールから見ると、Tensorflow用の仮想環境は立ち上がっている
    • OK (問題なくimportできる) -> jupyter notebookからの読み込みが上手くいかない
(tensorflow) $ python
Python 3.6.0 |Anaconda custom (x86_64)| (default, Dec 23 2016, 13:19:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> from tensorflow.examples.tutorials.mnist import input_data
  • Anaconda navigatorを再起動して、もう一度試す

    • NG (同じ) -> 立ち上げっぱなしが原因ではない
  • tensorflow仮想環境から直接 jupyter notebook を立ち上げてみる

    • NG (同じ) -> 立ち上げる場所が原因ではない
  • tensorflow仮想環境から直接立ち上げた jupyter notebook で import tensorflow1行を実行してみる

    • NG (同じ) -> サンプルコードが間違っているわけではない
  • どのjupyter notebookが起動されているのか?

    • /Users/kz/anaconda/envs/tensorflow/bin/jupyter_mac.command ; exit;
    • OK -> tensorflow環境のjupyter notebookが起動されている
  • tensorflowインストール時のpipがどこに紐付いていた?

    • OK -> tensorflow環境に紐ついている
$ which pip
/Users/kz/anaconda/envs/tensorflow/bin/pip
(tensorflow) $ conda install jupyter notebook

Package plan for installation in environment /Users/kz/anaconda/envs/tensorflow:
(tensorflow) $ which jupyter notebook
/Users/kz/anaconda/envs/tensorflow/bin/jupyter
(tensorflow) $ jupyter notebook
[I 18:03:32.381 NotebookApp] Serving notebooks from local directory: /Users/kz
  • conda listでtensorflowが出るか?
    • NG -> あれ?出てこない?
(tensorflow) $ conda list | grep tensorflow
# packages in environment at /Users/kz/anaconda/envs/tensorflow:
  • そうだ。pipでインストールしたから、condaには登録されていない
    • ここに原因があるのでは?
(tensorflow) $ echo $TF_PYTHON_URL
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py3-none-any.whl
(tensorflow) $ pip install --ignore-installed --upgrade $TF_PYTHON_URL
(tensorflow) $ pip freeze | grep tensorflow
tensorflow==1.1.0
  • 初心に帰って、conda仮想環境を立ち上げ直してみる
    • NG -> 変わらない
(tensorflow) $ source deactivate tensorflow
$ source activate tensorflow
(tensorflow) $ 
$ source activate tensorflow

(tensorflow) $ jupyter kernelspec list
Available kernels:
  python3    /Users/kz/anaconda/envs/tensorflow/share/jupyter/kernels/python3

(tensorflow) $ pip install ipykernel
Requirement already satisfied: ipykernel in ./anaconda/envs/tensorflow/lib/python3.6/site-packages
(tensorflow) $ python -m ipykernel install --user --name tensorflow --display-name "conda env tensorflow"

(tensorflow) $ jupyter kernelspec list
Available kernels:
  tensorflow    /Users/kz/Library/Jupyter/kernels/tensorflow
  python3       /Users/kz/anaconda/envs/tensorflow/share/jupyter/kernels/python3

(tensorflow) $ jupyter notebook
-m: モジュールを指定して実行
install: カーネルをインストールする
--user: 現在ログイン中のユーザにインストール
--name: カーネルスペックの名前を指定
--display-name: 表示されるカーネルの名前を指定
  • condaのバージョンを下げる
    • conda 4.3.17 , Python 3.6.1 を conda 4.2.16 , Python 3.6.1 へ
    • NG
(tensorflow) $ conda --version
conda 4.3.17
(tensorflow) $ conda install conda=4.2
Fetching package metadata ...........


InstallError: Install error: Error: 'conda' can only be installed into the root environment


(tensorflow) $ source deactivate tensorflow
$ conda --version
conda 4.3.17
$ conda install conda=4.2
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment /Users/kz/anaconda:

The following packages will be DOWNGRADED due to dependency conflicts:

    conda: 4.3.17-py36_0 --> 4.2.16-py36_0

Proceed ([y]/n)? y

conda-4.2.16-p 100% |######################################################################| Time: 0:00:00 428.78 kB/s
$ conda --version
conda 4.2.16
  • 念押しでtensorflowをもう一度インストー
    • できた。。。。
$ pip install tensorflow
Collecting tensorflow
  Downloading tensorflow-1.1.0-cp36-cp36m-macosx_10_11_x86_64.whl (31.3MB)
...
Installing collected packages: numpy, protobuf, werkzeug, tensorflow
Successfully installed numpy-1.12.1 protobuf-3.3.0 tensorflow-1.1.0 werkzeug-0.12.1