Python jupyter 中没有名为 tensorflow 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38221181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No module named tensorflow in jupyter
提问by Skotti Bendler
I have some imports in my jupyter notebook and among them is tensorflow:
我的 jupyter 笔记本中有一些导入,其中包括 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:
我可以在我的电脑上导入 tensorflow:
>>> import tensorflow as tf
>>>
So I'm confused why this is another situation in notebook?
所以我很困惑为什么这是笔记本中的另一种情况?
回答by Idel Pivnitskiy
If you installed a TensorFlow as it said in official documentation: https://www.tensorflow.org/versions/r0.10/get_started/os_setup.html#overview
如果您按照官方文档中的说明安装了 TensorFlow:https: //www.tensorflow.org/versions/r0.10/get_started/os_setup.html#overview
I mean creating an environment called tensorflowand tested your installation in python, but TensorFlow can not be imported in jupyter, you have to install jupyter in your tensorflow environment too:
我的意思是创建一个名为tensorflow的环境并在 python 中测试你的安装,但是 TensorFlow 不能在 jupyter 中导入,你也必须在你的 tensorflow 环境中安装 jupyter:
conda install jupyter notebook
After that I run a jupyter and it can import TensorFlow too:
之后我运行了一个 jupyter,它也可以导入 TensorFlow:
jupyter notebook
回答by alif
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:
Jupyter 在 conda 环境下运行,因为您的 tensorflow 安装位于 conda 之外。为了在 conda 虚拟环境下安装 tensorflow,请在终端中运行以下命令:
conda install -c conda-forge tensorflow
回答by alphaleonis
I had the same problem, and solved it by looking at the output of:
我遇到了同样的问题,并通过查看以下输出解决了它:
jupyter kernelspec list
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
.
请注意,该路径指向用户的 Jupyter 内核。要在 Anaconda 环境中使用它,它需要指向您正在使用的 conda env,并且看起来像Anaconda3\envs\Env_Name\share\jupyter\kernels\python3
.
So, to remove the Jupyter kernelspec, just use:
因此,要删除 Jupyter 内核规范,只需使用:
jupyter kernelspec remove python3
jupyter kernelspec remove python3
or jupyter kernelspec remove python2
if you're using python 2
或者jupyter kernelspec remove python2
如果您使用的是 python 2
Now, the output of jupyter kernelspec list
should point to the correct kernel.
现在,输出jupyter kernelspec list
应该指向正确的内核。
See https://github.com/jupyter/notebook/issues/397for more information about this.
有关这方面的更多信息,请参阅https://github.com/jupyter/notebook/issues/397。
回答by Navnit Shukla
This is what I did to fix this issue -
这就是我为解决这个问题所做的 -
I installed tensorflow for windows by using below link -
我使用以下链接为 Windows 安装了 tensorflow -
https://www.tensorflow.org/install/install_windows
https://www.tensorflow.org/install/install_windows
Once done - I activated tensorflow by using below command -
完成后 - 我使用以下命令激活了 tensorflow -
C:> activate tensorflow (tensorflow)C:> # Your prompt should change
C:> activate tensorflow (tensorflow)C:> # 你的提示应该改变
Once done I ran below command -
完成后,我运行以下命令 -
(tensorflow)C:> conda install notebook
(tensorflow)C:> conda 安装笔记本
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
jupyter_client 100% |#############################| 时间:0:00:00 6.77 MB/s nbformat-4.4.0 100% |############################ | 时间:0:00:00 8.10 MB/s ipykernel-4.6。100% |#############################| 时间:0:00:00 9.54 MB/s nbconvert-5.2。100% |#############################| 时间:0:00:00 9.59 MB/s notebook-5.0.0 100% |############################# | 时间:0:00:00 8.24 MB/s
Once done I ran command
完成后我运行命令
(tensorflow)C:>jupyter notebook
(tensorflow)C:>jupyter 笔记本
It opened new Juypter window and able to Run fine -
它打开了新的 Juypter 窗口并能够正常运行 -
import tensorflow as tf
将张量流导入为 tf
回答by Kelemu
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
当 Jupyter Notebook 可能从默认启动但能够导入 tensorflow 和 keras 库时,问题可能会出现,因此您必须像安装库一样安装 jupyter notebook
pip install jupyter
pip 安装 jupyter
回答by Salih Osman
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"
我能够通过以下方式在 Windows 上的 Jupyter notebook 中加载 tensorflow:首先执行 conda create tensorflow install,然后在命令提示符下激活 tensorflow,然后从命令行执行“Jupyter notebook”。Tensorflow 在笔记本上导入,没有错误。但是,我无法导入“Pandas”和“Matplotlib, ....etc”
回答by Gaurav Sitaula
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 Promptand 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.
我也有同样的问题很长一段时间。我想在 Windows 10 中的 jupyter notebook 中导入 tensorflow。我遵循了所有建议的说明和命令,但它在命令提示符下不起作用。最后,我在Anaconda Prompt 上尝试了这个命令并且成功了。如果您在 Anaconda 中使用 jupyter notebook,则转到 Windows 搜索终端并键入“Anaconda Prompt”,然后在其中键入以下命令,它将在 jupyter notebook 中安装 tensorflow。
conda install -c conda-forge tensorflow
回答by Ghafoor
As suggested by @J?rg, if you have more than one kernel spec. You have to see the path it points to. In my case, it is actually the path that was to be corrected.
正如@J?rg 所建议的,如果您有多个内核规范。你必须看到它指向的路径。就我而言,它实际上是要更正的路径。
When I created TensorFlow
virtual env, the spec had the entry for python which was pointing to base
env. Thus by changing W:\\miniconda\\python.exe
to W:\\miniconda\\envs\\tensorflow\\python.exe
solved the problem.
当我创建TensorFlow
虚拟 env 时,规范中有指向base
env 的python 条目。从而通过改变W:\\miniconda\\python.exe
来W:\\miniconda\\envs\\tensorflow\\python.exe
解决问题。
So it is worth looking at your kernel spec. Delete that is not needed and keep those you want. Then look inside the JSON files where the path is given and change if needs be. I hope it helps.
因此,值得查看您的内核规范。删除不需要的,保留你想要的。然后查看给出路径的 JSON 文件,并根据需要进行更改。我希望它有帮助。
回答by Vishal Tyagi
There are two ways to fix this issue.
有两种方法可以解决此问题。
- The foremost way is to create a new virtual environment and install all dependencies like jupyter notebook, tensorflowetc.
- 最重要的方法是创建一个新的虚拟环境并安装所有依赖项,如jupyter notebook、tensorflow等。
conda install jupyter notebook
conda install jupyter notebook
conda install -c conda-forge tensorflow
conda install -c conda-forge tensorflow
- The other way around is to install tensorflow in the current environment (base or any activated environment).
- 另一种方法是在当前环境(基础或任何激活的环境)中安装 tensorflow。
conda install -c conda-forge tensorflow
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:
注意:建议为每个新项目创建一个新的虚拟环境。可以在此处找到如何使用 conda 创建和管理虚拟环境的详细信息:
https://conda.io/docs/user-guide/tasks/manage-environments.html
https://conda.io/docs/user-guide/tasks/manage-environments.html
回答by Deep Nirmal
Conda environment fetches the tensorflow package from the main system site-packages.
Conda 环境从主系统站点包中获取 tensorflow 包。
Step 1: Just deactivate conda environment
第 1 步:只需停用 conda 环境
conda deactivate
pip install tensorflow
Step 2: Switch back to conda environment
第 2 步:切换回 conda 环境
conda activate YOUR_ENV_NAME
jupyter notebook
Step 3: Run the cell with import tensorflow
you should be able to import.
第 3 步:运行单元格,import tensorflow
您应该可以导入。
Thanks
谢谢