LoginSignup
3
1

More than 3 years have passed since last update.

Anaconda上のjupyter notebookでtensorflowが動かない

Posted at

問題点

仮想環境から起動したjupyter notebook上で仮想環境にインストールしたパッケージがimportできない

解決策

仮想環境にもjupyterをインストールする。

例. tensorflowが動かない

C:\Users\ktanaka> conda create -n tensorflow python
C:\Users\ktanaka> conda activate tensorflow
(tensorflow) C:\Users\ktanaka> pip install --ignore-installed --upgrade tensorflow
(tensorflow) C:\Users\ktanaka> jupyter notebook

1行目:tensorflowという名前でpythonだけがインストールされた仮想環境を構築
2行目:構築した仮想環境をactivate
3行目:tensorflowをインストール
4行目:jupyter notebookを起動

仮想環境にjupyterがインストールされていないためbaseのjupyter notebookが起動する。
(仮想環境のパスが優先されているだけなので、仮想環境上でツールが見つからない場合、パスに登録されているbase環境のツールが起動する)

import tensorflow as tf

起動したJupyter notebook上でtensorflowをimportしようとしても・・・

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-98d4364fe1cb> in <module>
----> 1 import tensorflow as tf

ModuleNotFoundError: No module named 'tensorflow'

baseの環境にはtensorflowがimportされていないのでエラーが出る。

(tensorflow) C:\Users\ktanaka>python
Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>

対話型のpythonだと仮想環境にインストールされたpythonが起動するので問題なくimportできる。

(tensorflow) C:\Users\ktanaka> conda install jupyter
(tensorflow) C:\Users\ktanaka> jupyter notebook

1行目:仮想環境上でjupyterをインストールすることで
2行目:仮想環境上のjupyter nootebookが起動する。

import tensorflow as tf

仮想環境にインストールしたtensorflowがインポートできる。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1