将我的jupyter笔记本设置为使用python版本的环境

时间:2018-10-31 11:37:10

标签: python anaconda jupyter-notebook

我从anaconda环境中打开了一个jupyter笔记本,但是内核使用的是默认版本的python,而不是此环境中定义的python(安装了我安装的所有pthe软件包)。

如何更改它?

(myEnv) okoub@my-mac:~/Desktop$jupyter notebook
[I 13:35:46.644 NotebookApp] The port 8888 is already in use, trying another port.
[I 13:35:46.646 NotebookApp] The port 8889 is already in use, trying another port.
[I 13:35:46.675 NotebookApp] JupyterLab alpha preview extension loaded from /Users/okoub/anaconda3/lib/python3.6/site-packages/jupyterlab

我正在使用MAC。

在环境中安装jupyter之后:

source activate myEnv
conda install jupyter
jupyter notebook

我得到了错误:

File "/Users/okoub/anaconda3/envs/myEnv/lib/python3.5/site-packages/prompt_toolkit/completion/base.py", line 4, in <module>
    from prompt_toolkit.eventloop import generator_to_async_generator, AsyncGeneratorItem
ImportError: cannot import name 'generator_to_async_generator'
[W 14:54:56.587 NotebookApp] KernelRestarter: restart failed
[W 14:54:56.587 NotebookApp] Kernel dcc36e3e-9352-402f-a513-ca1dca30e460 died, removing from map.
[W 14:55:41.612 NotebookApp] Timeout waiting for kernel_info reply from dcc36e3e-9352-402f-a513-ca1dca30e460
[E 14:55:41.616 NotebookApp] Error opening stream: HTTP 404: Not Found (Kernel does not exist: dcc36e3e-9352-402f-a513-ca1dca30e460)
[I 14:56:41.524 NotebookApp] Saving file at /my_notebook.ipynb

2 个答案:

答案 0 :(得分:2)

您正在使用安装在主要anaconda安装路径(/Users/okoub/anaconda3/lib/python3.6/site-packages/jupyterlab)中的jupyter。如果要在环境中使用jupyter,请将其安装在所需的环境中。

source activate myEnv
conda install jupyter
jupyter notebook

如果您已经在环境中安装了jupyter,请在运行jupyter notebook之前不要忘记激活它。

抱歉,我不知道MAC上激活conda环境的方式是否与Linux相同

跟随OP的修改:

您似乎在prompt_toolkit中出错。您可以尝试使用conda重新安装它吗?

答案 1 :(得分:1)

运行conda install nb_conda_kernels,然后重新启动jupyter。现在,右上角应该有一个附加选项,可以选择笔记本应使用哪个conda env来执行代码。这是IMO上最方便的解决方案,因为您可以在同一个项目中拥有多个笔记本,这些笔记本可以在各自的环境下运行,而不必始终source activate

在此处了解更多信息:https://github.com/Anaconda-Platform/nb_conda_kernels