No module named tensorflow in jupyter

自闭症网瘾萝莉.ら 提交于 2019-12-28 11:14:23

问题


I have some imports in my jupyter notebook and among them is tensorflow:

ImportError                               Traceback (most recent call last)
<ipython-input-2-482704985f85> in <module>()
      4 import numpy as np
      5 import six.moves.copyreg as copyreg
----> 6 import tensorflow as tf
      7 from six.moves import cPickle as pickle
      8 from six.moves import range

ImportError: No module named tensorflow

I have it on my computer, in a special enviroment and all connected stuff also:

Requirement already satisfied (use --upgrade to upgrade): tensorflow in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.10.0 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): protobuf==3.0.0b2 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.10.1 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): wheel in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./setuptools-23.0.0-py2.7.egg (from protobuf==3.0.0b2->tensorflow)

I can import tensorflow on my computer:

>>> import tensorflow as tf
>>> 

So I'm confused why this is another situation in notebook?


回答1:


If you installed a TensorFlow as it said in official documentation: https://www.tensorflow.org/versions/r0.10/get_started/os_setup.html#overview

I mean creating an environment called tensorflow and tested your installation in python, but TensorFlow can not be imported in jupyter, you have to install jupyter in your tensorflow environment too:

conda install jupyter notebook

After that I run a jupyter and it can import TensorFlow too:

jupyter notebook



回答2:


Jupyter runs under the conda environment where as your tensorflow install lives outside conda. In order to install tensorflow under the conda virtual environment run the following command in your terminal:

 conda install -c conda-forge tensorflow 



回答3:


I had the same problem, and solved it by looking at the output of:

jupyter kernelspec list

which outputs the kernel information:

python2 /Users/Username/Library/Jupyter/kernels/python2 
python3 /Users/Username/Library/Jupyter/kernels/python3

Notice that the path points to the Jupyter kernel for the user. To use it within the the Anaconda environment, it needs to point to the conda env you are using, and look something like Anaconda3\envs\Env_Name\share\jupyter\kernels\python3.

So, to remove the Jupyter kernelspec, just use:

jupyter kernelspec remove python3

or jupyter kernelspec remove python2 if you're using python 2

Now, the output of jupyter kernelspec list should point to the correct kernel.

See https://github.com/jupyter/notebook/issues/397 for more information about this.




回答4:


This is what I did to fix this issue -

I installed tensorflow for windows by using below link -

https://www.tensorflow.org/install/install_windows

Once done - I activated tensorflow by using below command -

C:> activate tensorflow (tensorflow)C:> # Your prompt should change

Once done I ran below command -

(tensorflow)C:> conda install notebook

Fetching package metadata ........... Solving package specifications: .

Package plan for installation in environment

The following NEW packages will be INSTALLED:

bleach:              1.5.0-py35_0
colorama:            0.3.9-py35_0
decorator:           4.1.2-py35_0
entrypoints:         0.2.3-py35_0
html5lib:            0.9999999-py35_0
ipykernel:           4.6.1-py35_0
 ----
 ---

jupyter_client 100% |###############################| Time: 0:00:00 6.77 MB/s nbformat-4.4.0 100% |###############################| Time: 0:00:00 8.10 MB/s ipykernel-4.6. 100% |###############################| Time: 0:00:00 9.54 MB/s nbconvert-5.2. 100% |###############################| Time: 0:00:00 9.59 MB/s notebook-5.0.0 100% |###############################| Time: 0:00:00 8.24 MB/s

Once done I ran command

(tensorflow)C:>jupyter notebook

It opened new Juypter window and able to Run fine -

import tensorflow as tf




回答5:


the problem may when the Jupyter notebook may launching from the default but for able to import tensorflow and keras libraries so you have to install jupyter notebook like what you have installed the libraries

pip install jupyter




回答6:


I was able to load tensorflow in Jupyter notebook on Windows by: first do conda create tensorflow install, then activate tensorflow at the command prompt , then execute "Jupyter notebook" from command line. Tensorflow imports at the notebook with no error. However, I was unable to import "Pandas" &"Matplotlib, ....etc"




回答7:


I also had the same problem for a long time. I wanted to import tensorflow inside the jupyter notebook within windows 10. I followed all the instructions and commands that were suggested and it was not working from the command prompt. Finally, I tried this command with the Anaconda Prompt and it worked successfully. If you are using jupyter notebook within Anaconda then go goto the windows search terminal and type "Anaconda Prompt" and inside it type following command, It will install the tensorflow inside the jupyter notebook.

conda install -c conda-forge tensorflow



回答8:


There are two ways to fix this issue.

  1. The foremost way is to create a new virtual environment and install all dependencies like jupyter notebook, tensorflow etc.

conda install jupyter notebook

conda install -c conda-forge tensorflow

  1. The other way around is to install tensorflow in the current environment (base or any activated environment).

conda install -c conda-forge tensorflow

Note: It is advisable to create a new virtual environment for every new project. The details how to create and manage virtual environment using conda can be find here:

https://conda.io/docs/user-guide/tasks/manage-environments.html




回答9:


Conda environment fetches the tensorflow package from the main system site-packages.

Step 1: Just deactivate conda environment

conda deactivate  

pip install tensorflow 

Step 2: Switch back to conda environment

conda activate YOUR_ENV_NAME

jupyter notebook

Step 3: Run the cell with import tensorflow you should be able to import.

Thanks




回答10:


Probably there is a problem with the TensorFlow in your environment. In my case, After installing some libs, my TensorFlow stopped working.

So I installed TensorFlow again using pip. like so:

just run

pip install tensorflow

then I re-imported it into my jupyter notebook as :

import tensorflow as ft

In case you want to install jupyter and base libs try this:

pip install jupyter tensorflow keras numpy scipy ipython pandas matplotlib sympy nose


来源:https://stackoverflow.com/questions/38221181/no-module-named-tensorflow-in-jupyter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!