Jupyter笔记本中的TensorFlow问题

时间:2021-07-13 22:50:19

I installed Jupyter notebooks in Ubuntu 14.04 via Anaconda earlier, and just now I installed TensorFlow. I would like TensorFlow to work regardless of whether I am working in a notebook or simply scripting. In my attempt to achieve this, I ended up installing TensorFlow twice, once using Anaconda, and once using pip. The Anaconda install works, but I need to preface any call to python with "source activate tensorflow". And the pip install works nicely, if start python the standard way (in the terminal) then tensorflow loads just fine.

我之前通过Anaconda在Ubuntu 14.04中安装了Jupyter笔记本,刚才我安装了TensorFlow。无论我是在笔记本电脑上工作还是只是编写脚本,我都希望TensorFlow能够正常工作。在我尝试实现这一目标时,我最终安装了TensorFlow两次,一次使用Anaconda,一次使用pip。 Anaconda安装工作,但我需要使用“source activate tensorflow”来预先调用python。并且pip安装工作得很好,如果以标准方式启动python(在终端中),那么tensorflow加载就好了。

My question is: how can I also have it work in the Jupyter notebooks?

我的问题是:我怎样才能在Jupyter笔记本中使用它?

This leads me to a more general question: it seems that my python kernel in Jupyter/Anaconda is separate from the python kernel (or environment? not sure about the terminology here) used system wide. It would be nice if these coincided, so that if I install a new python library, it becomes accessible to all the varied ways I have of running python.

这引出了一个更普遍的问题:似乎我的Jupyter / Anaconda中的python内核与系统范围内使用的python内核(或环境?不确定这里的术语)是分开的。如果这些重合,那将是很好的,所以如果我安装一个新的python库,它可以通过我运行python的各种方式访问​​。

11 个解决方案

#1


54  

Update

TensorFlow website supports five installations.

TensorFlow网站支持五种安装。

To my understanding, using Pip installation directly would be fine to import TensorFlow in Jupyter Notebook (as long as Jupyter Notebook was installed and there were no other issues) b/z it didn't create any virtual environments.

根据我的理解,直接使用Pip安装可以在Jupyter Notebook中导入TensorFlow(只要安装了Jupyter Notebook并且没有其他问题)b / z它没有创建任何虚拟环境。

Using virtualenv install and conda install would need to install jupyter into the newly created TensorFlow environment to allow TensorFlow to work in Jupyter Notebook (see the following original post section for more details).

使用virtualenv install和conda install需要在新创建的TensorFlow环境中安装jupyter以允许TensorFlow在Jupyter Notebook中工作(有关更多详细信息,请参阅以下原始帖子部分)。

I believe docker install may require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook (see this post).

我相信docker install可能需要在VirtualBox中设置一些端口才能使TensorFlow在Jupyter Notebook中工作(参见这篇文章)。

For installing from sources, it also depends on which environment the source code is built and installed into. If it's installed into a freshly created virtual environment or an virtual environment which didn't have Jupyter Notebook installed, it would also need to install Jupyter Notebook into the virtual environment to use Tensorflow in Jupyter Notebook.

对于从源安装,它还取决于构建和安装源代码的环境。如果将其安装到新创建的虚拟环境或未安装Jupyter Notebook的虚拟环境中,则还需要将Jupyter Notebook安装到虚拟环境中以在Jupyter Notebook中使用Tensorflow。

Original Post

To use tensorflow in Ipython and/or Jupyter(Ipython) Notebook, you'll need to install Ipython and Jupyter (after installing tensorflow) under the tensorflow activated environment.

要在Ipython和/或Jupyter(Ipython)Notebook中使用tensorflow,您需要在tensorflow激活环境下安装Ipython和Jupyter(在安装tensorflow之后)。

Before install Ipython and Jupyter under tensorflow environment, if you do the following commands in terminal:

在tensorflow环境下安装Ipython和Jupyter之前,如果在终端中执行以下命令:

username$ source activate tensorflow

(tensorflow)username$ which ipython
(tensorflow)username$ /Users/username/anaconda/bin/ipython

(tensorflow)username$ which jupyter
(tensorflow)username$ /Users/username/anaconda/bin/jupyter

(tensorflow)username$ which python
(tensorflow)username$ /User/username//anaconda/envs/tensorflow/bin/python

This is telling you that when you open python from terminal, it is using the one installed in the "environments" where tensorflow is installed. Therefore you can actually import tensorflow successfully. However, if you are trying to run ipython and/or jupyter notebook, these are not installed under the "environments" equipped with tensorflow, hence it has to go back to use the regular environment which has no tensorflow module, hence you get an import error.

这告诉你,当你从终端打开python时,它正在使用安装了tensorflow的“环境”中安装的python。因此,您实际上可以成功导入tensorflow。但是,如果你试图运行ipython和/或jupyter笔记本,这些不是安装在配备tensorflow的“环境”下,因此它必须返回使用没有tensorflow模块的常规环境,因此你得到一个导入错误。

You can verify this by listing out the items under envs/tensorflow/bin directory:

您可以通过列出envs / tensorflow / bin目录下的项来验证这一点:

(tensorflow) username$ ls /User/username/anaconda/envs/tensorflow/bin/

You will see that there are no "ipython" and/or "jupyer" listing out.

您将看到没有列出的“ipython”和/或“jupyer”。

To use tensorflow with Ipython and/or Jupyter notebook, simply install them into the tensorflow environment:

要在Ipython和/或Jupyter笔记本中使用tensorflow,只需将它们安装到tensorflow环境中:

(tensorflow) username$ conda install ipython
(tensorflow) username$ pip install jupyter #(use pip3 for python3)

After installing them, there should be a "jupyer" and a "ipython" show up in the envs/tensorflow/bin/ directory.

安装它们之后,应该在envs / tensorflow / bin /目录中显示“jupyer”和“ipython”。

Notes: Before trying to import tensorflow module in jupyter notebook, try close the notebook. And "source deactivate tensorflow" first, and then reactivate it ("source activate tensorflow") to make sure things are "on the same page". Then reopen the notebook and try import tensorflow. It should be import successfully (worked on mine at least).

注意:在尝试在jupyter笔记本中导入tensorflow模块之前,请尝试关闭笔记本。并且首先将“source deactivate tensorflow”重新激活(“source activate tensorflow”)以确保“在同一页面上”。然后重新打开笔记本并尝试导入tensorflow。它应该成功导入(至少在我的工作)。

#2


5  

i used these following which in virtualenv.

我在virtualenv中使用了以下这些。

pip3 install --ignore-installed ipython
pip3 install --ignore-installed jupyter

This re-installs both ipython and jupyter notebook in my tensorflow virtual environment. You can verify it after installation by which ipython and which jupyter. The bin will be under the virtual env.

这会在我的tensorflow虚拟环境中重新安装ipython和jupyter笔记本。您可以在安装后通过ipython和哪个jupyter验证它。垃圾箱将在虚拟环境下。

NOTE I am using python 3.*

注意我使用的是python 3. *

#3


4  

I have another solution that you don't need to source activate tensorflow before using jupyter notebook every time.

我有另一个解决方案,你不需要每次使用jupyter笔记本之前都要激活tensorflow。

Partion 1

Firstly, you should ensure you have installed jupyter in your virtualenv. If you have installed, you can skip this section (Use which jupyter to check). If you not, you could run source activate tensorflow, and then install jupyter in your virtualenv by conda install jupyter. (You can use pip too.)

首先,你应该确保你已经在virtualenv中安装了jupyter。如果已安装,则可以跳过此部分(使用哪个jupyter进行检查)。如果你没有,你可以运行source activate tensorflow,然后通过conda install jupyter在你的virtualenv中安装jupyter。 (你也可以用pip。)

Partion 2

1.From within your virtualenv, run

1.从你的virtualenv中,运行

username$ source activate tensorflow
(tensorflow)username$ ipython kernelspec install-self --user

This will create a kernelspec for your virtualenv and tell you where it is:

这将为你的virtualenv创建一个kernelspec并告诉你它在哪里:

(tensorflow)username$ [InstallNativeKernelSpec] Installed kernelspec pythonX in /home/username/.local/share/jupyter/kernels/pythonX

Where pythonX will match the version of Python in your virtualenv.

其中pythonX将与virtualenv中的Python版本相匹配。

2.Copy the new kernelspec somewhere useful. Choose a kernel_name for your new kernel that is not python2 or python3 or one you've used before and then:

2.在有用的地方复制新的kernelspec。为你的新内核选择一个不是python2或python3的kernel_name,或者你以前用过的内核:

(tensorflow)username$ mkdir -p ~/.ipython/kernels
(tensorflow)username$ mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>

3.If you want to change the name of the kernel that IPython shows you, you need to edit ~/.ipython/kernels/<kernel_name>/kernel.json and change the JSON key called display_name to be a name that you like.

3.如果要更改IPython向您显示的内核名称,则需要编辑〜/ .ipython / kernels / /kernel.json并将名为display_name的JSON键更改为您喜欢的名称。

4.You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.

4.您现在应该能够在IPython笔记本菜单中看到您的内核:内核 - >更改内核并且能够切换到它(您可能需要在页面出现之前刷新页面)。 IPython将记住从那时起用于该笔记本的内核。

Reference.

#4


2  

Your Anaconda install probably went to different directory than your Python install

您的Anaconda安装可能会转到与Python安装不同的目录

For instance on my machine I can find location here

例如,在我的机器上,我可以在这里找到位置

yaroslavvb-macbookpro:~ yaroslavvb$ which ipython
/Users/yaroslavvb/anaconda/bin/ipython

When you type python, it tries to find it in PATH going in left-to-right order. So you may have another version of python in a folder before Anaconda folder, and it'll use that. To fix, you can do export PATH=.... to change the path, and put Anaconda directory in front, so that it takes python from there instead of the default, ie

当你键入python时,它会尝试以从左到右的顺序在PATH中找到它。所以你可能在Anaconda文件夹之前的文件夹中有另一个版本的python,它会使用它。要修复,你可以导出PATH = ....来改变路径,并将Anaconda目录放在前面,这样就可以从那里获取python而不是默认值,即

export PATH=/Users/yaroslavvb/anaconda/bin:$PATH

#5


2  

Here is what I did to enable tensorflow in Anaconda -> Jupyter.

以下是我在Anaconda中启用张量流的方法 - > Jupyter。

  1. Install Tensorflow using the instructions provided at
  2. 使用提供的说明安装Tensorflow

  3. Go to /Users/username/anaconda/env and ensure Tensorflow is installed
  4. 转至/ Users / username / anaconda / env并确保已安装Tensorflow

  5. Open the Anaconda navigator and go to "Environments" (located in the left navigation)
  6. 打开Anaconda导航器并转到“环境”(位于左侧导航栏中)

  7. Select "All" in teh first drop down and search for Tensorflow
  8. 首先选择“全部”下拉并搜索Tensorflow

  9. If its not enabled, enabled it in the checkbox and confirm the process that follows.
  10. 如果未启用,则在复选框中启用它并确认后面的过程。

  11. Now open a new Jupyter notebook and tensorflow should work
  12. 现在打开一个新的Jupyter笔记本和tensorflow应该工作

#6


1  

I installed PIP with Conda conda install pip instead of apt-get install python-pip python-dev.

我用Conda conda install pip而不是apt-get install python -pip python-dev安装了PIP。

Then installed tensorflow use Pip Installation:

然后安装tensorflow使用Pip安装:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7 
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below. 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl

...

pip install --upgrade $TF_BINARY_URL

pip install --upgrade $ TF_BINARY_URL

Then it will work in jupyter notebook.

然后它将在jupyter笔记本中工作。

#7


0  

I think your question is very similar with the question post here. Windows 7 jupyter notebook executing tensorflow. As Yaroslav mentioned, you can try

我认为你的问题与这里的问题帖子非常相似。 Windows 7 jupyter笔记本执行tensorflow。正如雅罗斯拉夫所说,你可以试试

conda install -c http://conda.anaconda.org/jjhelmus tensorflow .

conda install -c http://conda.anaconda.org/jjhelmus tensorflow。

#8


0  

I had a similar issue when using a custom Ubuntu 16 image. The problem was related to an existing version of numpy that was already installed on my system.

使用自定义Ubuntu 16映像时,我遇到了类似的问题。问题与我系统上已安装的现有numpy版本有关。

I initially tried

我最初尝试过

sudo pip3 install tensorflow

This resulted in the following exception:

这导致以下异常:

Exception: Traceback (most recent call last): File "/anaconda/envs/py35/lib/python3.5/shutil.py", line 538, in move os.rename(src, real_dst) PermissionError: [Errno 13] Permission denied: '/anaconda/envs/py35/lib/python3.5/site-packages/numpy' -> '/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/numpy'

例外:回溯(最近通话最后一个):文件 “/anaconda/envs/py35/lib/python3.5/shutil.py”,线路538,在移动os.rename(SRC,real_dst)PermissionError:[错误13]权限否认: '/anaconda/envs/py35/lib/python3.5/site-packages/numpy' - >“/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/ numpy的”

The docs advise that if you encounter any issues with this command to try the following:

文档建议如果您遇到此命令的任何问题,请尝试以下操作:

sudo pip3 install --upgrade \ 
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

However, my system was unable to locate pip3

但是,我的系统无法找到pip3

sudo: pip3 command not found

sudo:找不到pip3命令

The ulitmate solution was to create a symlink for pip3

ulitmate解决方案是为pip3创建一个符号链接

sudo ln -s /anaconda/envs/py35/bin/pip3.5 /usr/local/bin/pip3

Finally, the following command worked without trouble

最后,以下命令没有遇到麻烦

sudo /usr/local/bin/pip3 install --upgrade \ 
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

I verified the installation in the terminal and also verified a successful import in my Jupyter Notebook

我验证了终端中的安装,并验证了我在Jupyter笔记本中的成功导入

import tensorflow as tf

#9


0  

The accepted answer (by Zhongyu Kuang) has just helped me out. Here I've create an environment.yml file that enables me to make this conda / tensorflow installation process repeatable.

接受的答案(中宇光)刚刚帮助了我。在这里,我创建了一个environment.yml文件,使我能够使这个conda / tensorflow安装过程可重复。

Step 1 - create a Conda environment.yml File

environment.yml looks like this:

environment.yml看起来像这样:

name: hello-tensorflow
dependencies:
  - python=3.6
  - jupyter
  - ipython
  - pip:
    - https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp36-cp36m-linux_x86_64.whl

Note:

  • Simply replace the name to whatever you want. (mine is hello-tensorflow)
  • 只需将名称替换为您想要的任何名称。 (我的是hello-tensorflow)

  • Simply replace the python version to whatever you want. (mine is 3.6)
  • 只需将python版本替换为您想要的任何内容。 (我的是3.6)

  • Simply replace the tensorflow pip install URL to whatever you want (mine is the Tensorflow URL where Python 3.6 with GPU support)
  • 只需将tensorflow pip安装URL替换为您想要的任何内容(我的是具有GPU支持的Python 3.6的Tensorflow URL)

Step 2 - create the Conda environment

With the environment.yml being in the current path you are on, this command creates the environment hello-tensorflow (or whatever you have renamed it to):

当environment.yml位于您所在的当前路径中时,此命令将创建环境hello-tensorflow(或者您已将其重命名为的任何内容):

conda env create -f environment.yml

Step 3: source activate

Activate the newly created environment:

激活新创建的环境:

source activate hello-tensorflow

Step 4 - which python / jupyter / ipython

which python...

(hello-tensorflow) $ which python
/home/johnny/anaconda3/envs/hello-tensorflow/bin/python

which jupyter...

(hello-tensorflow) $ which jupyter
/home/johnny/anaconda3/envs/hello-tensorflow/bin/jupyter

which ipython...

(hello-tensorflow) $ which ipython
/home/johnny/anaconda3/envs/hello-tensorflow/bin/ipython

Step 5

You should now be able to import tensorflow from python, jupyter (console / qtconsole / notebook, etc.) and ipython.

你现在应该可以从python,jupyter(console / qtconsole / notebook等)和ipython导入tensorflow。

#10


0  

I wonder if it is not enough to simply launch ipython from tensorflow environnement. That is 1) first activate tensorflow virtualenv with:

我想知道仅仅从tensorflow环境启动ipython还不够。那是1)首先激活tensorflow virtualenv:

source ~/tensorflow/bin/activate

2) launch ipython under tensorflow environnement

2)在tensorflow环境下启动ipython

(tensorflow)$ ipython notebook --ip=xxx.xxx.xxx.xxx

#11


0  

I found the solution from someone else's post. It is simple and works well!

我从其他人的帖子中找到了解决方案。它很简单,效果很好!

http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs

Just install the following in the Command Prompt and change kernel to Python 3 in Jupyter Notebook. It will import tensorflow successfully.

只需在命令提示符中安装以下内容,并在Jupyter Notebook中将内核更改为Python 3。它将成功导入tensorflow。

pip install tornado==4.5.3

pip install tornado == 4.5.3

pip install ipykernel==4.8.2

pip install ipykernel == 4.8.2

(Orginial post: https://github.com/tensorflow/tensorflow/issues/11851)

(Orginial帖子:https://github.com/tensorflow/tensorflow/issues/11851)

#1


54  

Update

TensorFlow website supports five installations.

TensorFlow网站支持五种安装。

To my understanding, using Pip installation directly would be fine to import TensorFlow in Jupyter Notebook (as long as Jupyter Notebook was installed and there were no other issues) b/z it didn't create any virtual environments.

根据我的理解,直接使用Pip安装可以在Jupyter Notebook中导入TensorFlow(只要安装了Jupyter Notebook并且没有其他问题)b / z它没有创建任何虚拟环境。

Using virtualenv install and conda install would need to install jupyter into the newly created TensorFlow environment to allow TensorFlow to work in Jupyter Notebook (see the following original post section for more details).

使用virtualenv install和conda install需要在新创建的TensorFlow环境中安装jupyter以允许TensorFlow在Jupyter Notebook中工作(有关更多详细信息,请参阅以下原始帖子部分)。

I believe docker install may require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook (see this post).

我相信docker install可能需要在VirtualBox中设置一些端口才能使TensorFlow在Jupyter Notebook中工作(参见这篇文章)。

For installing from sources, it also depends on which environment the source code is built and installed into. If it's installed into a freshly created virtual environment or an virtual environment which didn't have Jupyter Notebook installed, it would also need to install Jupyter Notebook into the virtual environment to use Tensorflow in Jupyter Notebook.

对于从源安装,它还取决于构建和安装源代码的环境。如果将其安装到新创建的虚拟环境或未安装Jupyter Notebook的虚拟环境中,则还需要将Jupyter Notebook安装到虚拟环境中以在Jupyter Notebook中使用Tensorflow。

Original Post

To use tensorflow in Ipython and/or Jupyter(Ipython) Notebook, you'll need to install Ipython and Jupyter (after installing tensorflow) under the tensorflow activated environment.

要在Ipython和/或Jupyter(Ipython)Notebook中使用tensorflow,您需要在tensorflow激活环境下安装Ipython和Jupyter(在安装tensorflow之后)。

Before install Ipython and Jupyter under tensorflow environment, if you do the following commands in terminal:

在tensorflow环境下安装Ipython和Jupyter之前,如果在终端中执行以下命令:

username$ source activate tensorflow

(tensorflow)username$ which ipython
(tensorflow)username$ /Users/username/anaconda/bin/ipython

(tensorflow)username$ which jupyter
(tensorflow)username$ /Users/username/anaconda/bin/jupyter

(tensorflow)username$ which python
(tensorflow)username$ /User/username//anaconda/envs/tensorflow/bin/python

This is telling you that when you open python from terminal, it is using the one installed in the "environments" where tensorflow is installed. Therefore you can actually import tensorflow successfully. However, if you are trying to run ipython and/or jupyter notebook, these are not installed under the "environments" equipped with tensorflow, hence it has to go back to use the regular environment which has no tensorflow module, hence you get an import error.

这告诉你,当你从终端打开python时,它正在使用安装了tensorflow的“环境”中安装的python。因此,您实际上可以成功导入tensorflow。但是,如果你试图运行ipython和/或jupyter笔记本,这些不是安装在配备tensorflow的“环境”下,因此它必须返回使用没有tensorflow模块的常规环境,因此你得到一个导入错误。

You can verify this by listing out the items under envs/tensorflow/bin directory:

您可以通过列出envs / tensorflow / bin目录下的项来验证这一点:

(tensorflow) username$ ls /User/username/anaconda/envs/tensorflow/bin/

You will see that there are no "ipython" and/or "jupyer" listing out.

您将看到没有列出的“ipython”和/或“jupyer”。

To use tensorflow with Ipython and/or Jupyter notebook, simply install them into the tensorflow environment:

要在Ipython和/或Jupyter笔记本中使用tensorflow,只需将它们安装到tensorflow环境中:

(tensorflow) username$ conda install ipython
(tensorflow) username$ pip install jupyter #(use pip3 for python3)

After installing them, there should be a "jupyer" and a "ipython" show up in the envs/tensorflow/bin/ directory.

安装它们之后,应该在envs / tensorflow / bin /目录中显示“jupyer”和“ipython”。

Notes: Before trying to import tensorflow module in jupyter notebook, try close the notebook. And "source deactivate tensorflow" first, and then reactivate it ("source activate tensorflow") to make sure things are "on the same page". Then reopen the notebook and try import tensorflow. It should be import successfully (worked on mine at least).

注意:在尝试在jupyter笔记本中导入tensorflow模块之前,请尝试关闭笔记本。并且首先将“source deactivate tensorflow”重新激活(“source activate tensorflow”)以确保“在同一页面上”。然后重新打开笔记本并尝试导入tensorflow。它应该成功导入(至少在我的工作)。

#2


5  

i used these following which in virtualenv.

我在virtualenv中使用了以下这些。

pip3 install --ignore-installed ipython
pip3 install --ignore-installed jupyter

This re-installs both ipython and jupyter notebook in my tensorflow virtual environment. You can verify it after installation by which ipython and which jupyter. The bin will be under the virtual env.

这会在我的tensorflow虚拟环境中重新安装ipython和jupyter笔记本。您可以在安装后通过ipython和哪个jupyter验证它。垃圾箱将在虚拟环境下。

NOTE I am using python 3.*

注意我使用的是python 3. *

#3


4  

I have another solution that you don't need to source activate tensorflow before using jupyter notebook every time.

我有另一个解决方案,你不需要每次使用jupyter笔记本之前都要激活tensorflow。

Partion 1

Firstly, you should ensure you have installed jupyter in your virtualenv. If you have installed, you can skip this section (Use which jupyter to check). If you not, you could run source activate tensorflow, and then install jupyter in your virtualenv by conda install jupyter. (You can use pip too.)

首先,你应该确保你已经在virtualenv中安装了jupyter。如果已安装,则可以跳过此部分(使用哪个jupyter进行检查)。如果你没有,你可以运行source activate tensorflow,然后通过conda install jupyter在你的virtualenv中安装jupyter。 (你也可以用pip。)

Partion 2

1.From within your virtualenv, run

1.从你的virtualenv中,运行

username$ source activate tensorflow
(tensorflow)username$ ipython kernelspec install-self --user

This will create a kernelspec for your virtualenv and tell you where it is:

这将为你的virtualenv创建一个kernelspec并告诉你它在哪里:

(tensorflow)username$ [InstallNativeKernelSpec] Installed kernelspec pythonX in /home/username/.local/share/jupyter/kernels/pythonX

Where pythonX will match the version of Python in your virtualenv.

其中pythonX将与virtualenv中的Python版本相匹配。

2.Copy the new kernelspec somewhere useful. Choose a kernel_name for your new kernel that is not python2 or python3 or one you've used before and then:

2.在有用的地方复制新的kernelspec。为你的新内核选择一个不是python2或python3的kernel_name,或者你以前用过的内核:

(tensorflow)username$ mkdir -p ~/.ipython/kernels
(tensorflow)username$ mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>

3.If you want to change the name of the kernel that IPython shows you, you need to edit ~/.ipython/kernels/<kernel_name>/kernel.json and change the JSON key called display_name to be a name that you like.

3.如果要更改IPython向您显示的内核名称,则需要编辑〜/ .ipython / kernels / /kernel.json并将名为display_name的JSON键更改为您喜欢的名称。

4.You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.

4.您现在应该能够在IPython笔记本菜单中看到您的内核:内核 - >更改内核并且能够切换到它(您可能需要在页面出现之前刷新页面)。 IPython将记住从那时起用于该笔记本的内核。

Reference.

#4


2  

Your Anaconda install probably went to different directory than your Python install

您的Anaconda安装可能会转到与Python安装不同的目录

For instance on my machine I can find location here

例如,在我的机器上,我可以在这里找到位置

yaroslavvb-macbookpro:~ yaroslavvb$ which ipython
/Users/yaroslavvb/anaconda/bin/ipython

When you type python, it tries to find it in PATH going in left-to-right order. So you may have another version of python in a folder before Anaconda folder, and it'll use that. To fix, you can do export PATH=.... to change the path, and put Anaconda directory in front, so that it takes python from there instead of the default, ie

当你键入python时,它会尝试以从左到右的顺序在PATH中找到它。所以你可能在Anaconda文件夹之前的文件夹中有另一个版本的python,它会使用它。要修复,你可以导出PATH = ....来改变路径,并将Anaconda目录放在前面,这样就可以从那里获取python而不是默认值,即

export PATH=/Users/yaroslavvb/anaconda/bin:$PATH

#5


2  

Here is what I did to enable tensorflow in Anaconda -> Jupyter.

以下是我在Anaconda中启用张量流的方法 - > Jupyter。

  1. Install Tensorflow using the instructions provided at
  2. 使用提供的说明安装Tensorflow

  3. Go to /Users/username/anaconda/env and ensure Tensorflow is installed
  4. 转至/ Users / username / anaconda / env并确保已安装Tensorflow

  5. Open the Anaconda navigator and go to "Environments" (located in the left navigation)
  6. 打开Anaconda导航器并转到“环境”(位于左侧导航栏中)

  7. Select "All" in teh first drop down and search for Tensorflow
  8. 首先选择“全部”下拉并搜索Tensorflow

  9. If its not enabled, enabled it in the checkbox and confirm the process that follows.
  10. 如果未启用,则在复选框中启用它并确认后面的过程。

  11. Now open a new Jupyter notebook and tensorflow should work
  12. 现在打开一个新的Jupyter笔记本和tensorflow应该工作

#6


1  

I installed PIP with Conda conda install pip instead of apt-get install python-pip python-dev.

我用Conda conda install pip而不是apt-get install python -pip python-dev安装了PIP。

Then installed tensorflow use Pip Installation:

然后安装tensorflow使用Pip安装:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7 
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below. 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl

...

pip install --upgrade $TF_BINARY_URL

pip install --upgrade $ TF_BINARY_URL

Then it will work in jupyter notebook.

然后它将在jupyter笔记本中工作。

#7


0  

I think your question is very similar with the question post here. Windows 7 jupyter notebook executing tensorflow. As Yaroslav mentioned, you can try

我认为你的问题与这里的问题帖子非常相似。 Windows 7 jupyter笔记本执行tensorflow。正如雅罗斯拉夫所说,你可以试试

conda install -c http://conda.anaconda.org/jjhelmus tensorflow .

conda install -c http://conda.anaconda.org/jjhelmus tensorflow。

#8


0  

I had a similar issue when using a custom Ubuntu 16 image. The problem was related to an existing version of numpy that was already installed on my system.

使用自定义Ubuntu 16映像时,我遇到了类似的问题。问题与我系统上已安装的现有numpy版本有关。

I initially tried

我最初尝试过

sudo pip3 install tensorflow

This resulted in the following exception:

这导致以下异常:

Exception: Traceback (most recent call last): File "/anaconda/envs/py35/lib/python3.5/shutil.py", line 538, in move os.rename(src, real_dst) PermissionError: [Errno 13] Permission denied: '/anaconda/envs/py35/lib/python3.5/site-packages/numpy' -> '/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/numpy'

例外:回溯(最近通话最后一个):文件 “/anaconda/envs/py35/lib/python3.5/shutil.py”,线路538,在移动os.rename(SRC,real_dst)PermissionError:[错误13]权限否认: '/anaconda/envs/py35/lib/python3.5/site-packages/numpy' - >“/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/ numpy的”

The docs advise that if you encounter any issues with this command to try the following:

文档建议如果您遇到此命令的任何问题,请尝试以下操作:

sudo pip3 install --upgrade \ 
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

However, my system was unable to locate pip3

但是,我的系统无法找到pip3

sudo: pip3 command not found

sudo:找不到pip3命令

The ulitmate solution was to create a symlink for pip3

ulitmate解决方案是为pip3创建一个符号链接

sudo ln -s /anaconda/envs/py35/bin/pip3.5 /usr/local/bin/pip3

Finally, the following command worked without trouble

最后,以下命令没有遇到麻烦

sudo /usr/local/bin/pip3 install --upgrade \ 
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

I verified the installation in the terminal and also verified a successful import in my Jupyter Notebook

我验证了终端中的安装,并验证了我在Jupyter笔记本中的成功导入

import tensorflow as tf

#9


0  

The accepted answer (by Zhongyu Kuang) has just helped me out. Here I've create an environment.yml file that enables me to make this conda / tensorflow installation process repeatable.

接受的答案(中宇光)刚刚帮助了我。在这里,我创建了一个environment.yml文件,使我能够使这个conda / tensorflow安装过程可重复。

Step 1 - create a Conda environment.yml File

environment.yml looks like this:

environment.yml看起来像这样:

name: hello-tensorflow
dependencies:
  - python=3.6
  - jupyter
  - ipython
  - pip:
    - https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp36-cp36m-linux_x86_64.whl

Note:

  • Simply replace the name to whatever you want. (mine is hello-tensorflow)
  • 只需将名称替换为您想要的任何名称。 (我的是hello-tensorflow)

  • Simply replace the python version to whatever you want. (mine is 3.6)
  • 只需将python版本替换为您想要的任何内容。 (我的是3.6)

  • Simply replace the tensorflow pip install URL to whatever you want (mine is the Tensorflow URL where Python 3.6 with GPU support)
  • 只需将tensorflow pip安装URL替换为您想要的任何内容(我的是具有GPU支持的Python 3.6的Tensorflow URL)

Step 2 - create the Conda environment

With the environment.yml being in the current path you are on, this command creates the environment hello-tensorflow (or whatever you have renamed it to):

当environment.yml位于您所在的当前路径中时,此命令将创建环境hello-tensorflow(或者您已将其重命名为的任何内容):

conda env create -f environment.yml

Step 3: source activate

Activate the newly created environment:

激活新创建的环境:

source activate hello-tensorflow

Step 4 - which python / jupyter / ipython

which python...

(hello-tensorflow) $ which python
/home/johnny/anaconda3/envs/hello-tensorflow/bin/python

which jupyter...

(hello-tensorflow) $ which jupyter
/home/johnny/anaconda3/envs/hello-tensorflow/bin/jupyter

which ipython...

(hello-tensorflow) $ which ipython
/home/johnny/anaconda3/envs/hello-tensorflow/bin/ipython

Step 5

You should now be able to import tensorflow from python, jupyter (console / qtconsole / notebook, etc.) and ipython.

你现在应该可以从python,jupyter(console / qtconsole / notebook等)和ipython导入tensorflow。

#10


0  

I wonder if it is not enough to simply launch ipython from tensorflow environnement. That is 1) first activate tensorflow virtualenv with:

我想知道仅仅从tensorflow环境启动ipython还不够。那是1)首先激活tensorflow virtualenv:

source ~/tensorflow/bin/activate

2) launch ipython under tensorflow environnement

2)在tensorflow环境下启动ipython

(tensorflow)$ ipython notebook --ip=xxx.xxx.xxx.xxx

#11


0  

I found the solution from someone else's post. It is simple and works well!

我从其他人的帖子中找到了解决方案。它很简单,效果很好!

http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs

Just install the following in the Command Prompt and change kernel to Python 3 in Jupyter Notebook. It will import tensorflow successfully.

只需在命令提示符中安装以下内容,并在Jupyter Notebook中将内核更改为Python 3。它将成功导入tensorflow。

pip install tornado==4.5.3

pip install tornado == 4.5.3

pip install ipykernel==4.8.2

pip install ipykernel == 4.8.2

(Orginial post: https://github.com/tensorflow/tensorflow/issues/11851)

(Orginial帖子:https://github.com/tensorflow/tensorflow/issues/11851)