jupyter で ModuleNotFoundError: No module named 'tensorflow' エラー
What?
- Installing TensorFlow on Mac OS X | TensorFlow に沿ってインストール
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
- 成功したパッケージ
- 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 tensorflow
1行を実行してみる- 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
- コマンドラインから Jupyter notebook をインストールし、立ち上げ
- NG (同じ) -> なんやこれJupyter notebookからだけ読めんやんけ
- 参考: python - No module named tensorflow in jupyter - Stack Overflow
(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) $
- 公式QAに頼る
- うまく紐付けできてない人がいた
- 参考: ImportError: No module named tensorflow - Can't install Tensorflow · Issue #5478 · tensorflow/tensorflow · GitHub
- Jupyterで利用可能なカーネルが増えた -> NG。。。
$ 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: 表示されるカーネルの名前を指定
(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