我的Jupyter笔记本电脑安装了python 2内核。我不明白为什么。我可能在安装的时候搞砸了。我已经安装了python 3。我怎么能把它加到木星上? 下面是默认的Jupyter使用python3 -m install Jupyter安装并在浏览器中使用Jupyter notebook打开的截图:


确保已经安装了ipykernel,并使用ipython kernel install将kernelspec放到python2的正确位置。然后为Python3安装ipython3内核。现在,无论您使用的是jupyter notebook、ipython notebook还是ipython3 notebook(后两种已弃用),您都应该能够在这两种内核之间进行选择。

注意,如果你想安装一个特定的Python可执行文件,你可以使用以下技巧:

path/to/python -m ipykernel install <options>

当使用环境(venv,conda,…)和<选项>让你命名你的内核时,这是有效的(参见——help)。所以你可以

conda create -n py36-test python=3.6
source activate py36-test
python -m ipykernel install --name py36-test
source deactivate

现在,在下拉菜单中可以看到名为py36-test的内核和其他内核。

参见使用Python 2。3. Python。IPython Notebook中的最新信息。


除了Python2之外,我还设法安装了Python3内核。我是这样做的:

在木星上打开一个新的笔记本 复制并运行这里的两个单元格:Enable-Python-3-kernel

最新的工作链接可以在这里找到。

实际代码为:

! mkdir -p ~/.ipython/kernels/python3
%%file ~/.ipython/kernels/python3/kernel.json

{
 "display_name": "IPython (Python 3)", 
 "language": "python", 
 "argv": [
  "python3", 
  "-c", "from IPython.kernel.zmq.kernelapp import main; main()", 
  "-f", "{connection_file}"
 ], 
 "codemirror_mode": {
  "version": 2, 
  "name": "ipython"
 }
}

我有Python 2.7,并希望能够在Jupyter内部切换到Python 3。

这些步骤在Windows Anaconda命令提示符上为我工作:

conda update conda
conda create -n py33 python=3.3 anaconda
activate py33
ipython kernelspec install-self
deactivate

现在,在使用Python2.7的常用命令打开ipython notebook之后,在创建新notebook时也可以使用Python3.3。


在ElementaryOS Freya(基于Ubuntu 14.04)上,其他答案都没有立即对我起作用;我得到了

[TerminalIPythonApp]警告|文件不存在:'kernelspec'

quickbug在Matt的回答中描述的错误。我首先要做的是:

Sudo apt-get安装pip3

安装ipython[所有]

这时你就可以运行Matt建议的命令了;即:ipython kernelspec install-self和ipython3 kernelspec install-self

现在,当我启动ipython notebook,然后打开一个notebook时,我能够从kernel菜单中选择Python 3内核。


如果你使用的是anaconda发行版,这对我来说是有效的(在macintosh上):

$ conda create -n py3k python=3 anaconda

$ source activate py3k

$ ipython kernelspec install-self

最后一个命令需要注意:

(py3k)Monas-MacBook-Pro:cs799 mona$ ipython kernelspec install-self
[TerminalIPythonApp] WARNING | Subcommand `ipython kernelspec` is deprecated and will be removed in future versions.
[TerminalIPythonApp] WARNING | You likely want to use `jupyter kernelspec` in the future
[InstallNativeKernelSpec] WARNING | `jupyter kernelspec install-self` is DEPRECATED as of 4.0. You probably want `ipython kernel install` to install the IPython kernelspec.
[InstallNativeKernelSpec] Installed kernelspec python3 in /usr/local/share/jupyter/kernels/python3
(py3k)Monas-MacBook-Pro:cs799 mona$ ipython kernel install 
Installed kernelspec python3 in /usr/local/share/jupyter/kernels/python3

按照上述步骤在OSX Yosemite中进行测试,并输入jupter notebook并在浏览器中创建一个新的notebook,您将看到以下截图:


Here's a Windows/non command line method I found, which worked for me: Find the folder where the kernel files are stored (on my machine - C:\ProgramData\jupyter\kernels - note that ProgramData is a hidden folder), create a copy of the existing kernel's folder, change the name and edit the json file within to point to the new kernel's directory. In this json you can also edit the kernel name that is displayed in ipython (e.g. instead of just python 2 you can specify 2.7.9 if you need to further distinguish for some reason).


使用Python2获得ipython笔记本(在Windows7上) 用pip install -U Jupyter升级到Jupyter 安装Python3 使用pip3 install Jupyter再次安装Jupyter 使用ipython3 kernelspec install-self安装Python3内核 我终于有两个工作的玉米粒了。


在Ubuntu 14.04上,我不得不使用之前答案的组合。

首先,安装pip3 安装python-pip3

然后用pip3安装jupyter Pip3安装jupyter

然后使用ipython3安装内核 Ipython3内核安装


解决方案在官方文档中有详细的说明:https://ipython.readthedocs.org/en/latest/install/kernel_install.html

我尝试了第一种方法。因为我已经安装了ipykernel,简单地运行python3 -m ipykernel install——user就解决了这个问题。


这对我在Ubuntu 16.04上是有效的:

python2 -m pip install ipykernel
python2 -m ipykernel install --user

python3 -m pip install ipykernel
python3 -m ipykernel install --user

参考文档: Python 2和3的内核。安装IPython内核- IPython Docs。


我用以下命令成功地在macOS El Capitan (ipython版本:4.1.0)上安装了python3内核。

python3 -m pip install ipykernel
python3 -m ipykernel install --user

你可以在jupyter kernelspec列表中看到所有已安装的内核。

更多信息可以在这里找到


打开终端(或CMD为窗口),然后执行以下命令: (在窗口中,在第二行中删除“source”。)

conda create -n py35 python=3.5
source activate py35
conda install notebook ipykernel
ipython kernel install --user --name=python3.5

我尝试了一些方法,但它不工作,然后我发现这个方法。 这对我很管用。希望能有所帮助。


对于jupyter/ipython的最新版本:使用jupyter kernelspec

完整文档:https://ipython.readthedocs.io/en/latest/install/kernel_install.html

列出当前内核

$ jupyter kernelspec list
Available kernels:
  python2    .../Jupyter/kernels/python2
  python3    .../Jupyter/kernels/python3

在我的例子中,python3内核设置被破坏了,因为py3.5链接不再存在,取而代之的是py3.6

添加/删除内核

删除:

$ jupyter kernelspec uninstall python3

添加一个新的: 使用你想要添加的Python并指向运行jupiter的Python:

$ /path/to/kernel/env/bin/python -m ipykernel install --prefix=/path/to/jupyter/env --name 'python-my-env'

更多例子见https://ipython.readthedocs.io/en/6.5.0/install/kernel_install.html#kernels-for-different-environments

列表:

$ jupyter kernelspec list
Available kernels:
  python3    /usr/local/lib/python3.6/site-packages/ipykernel/resources
  python2    /Users/stefano/Library/Jupyter/kernels/python2

道格:https://jupyter-client.readthedocs.io/en/latest/kernels.html kernelspecs

细节

可用的内核列在Jupyter DATA DIRECTORY的Kernels文件夹下(详情请参阅http://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html)。

例如,在macosx上,应该是/Users/YOURUSERNAME/Library/Jupyter/kernels/

内核被简单地描述为内核。Json文件,例如。/用户/我/图书馆/ Jupyter /内核/ python3 / kernel.json

{
 "argv": [
  "/usr/local/opt/python3/bin/python3.5",
  "-m",
  "ipykernel",
  "-f",
  "{connection_file}"
 ],
 "language": "python",
 "display_name": "Python 3"
}

您可以使用kernelspec命令(如上所述),而不是手动操作。以前可以通过ipython使用,现在可以通过jupyter (http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments - https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs)使用。

$ jupyter kernelspec help
Manage Jupyter kernel specifications.

Subcommands
-----------

Subcommands are launched as `jupyter kernelspec cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter kernelspec cmd -h`.

list
    List installed kernel specifications.
install
    Install a kernel specification directory.
uninstall
    Alias for remove
remove
    Remove one or more Jupyter kernelspecs by name.
install-self
    [DEPRECATED] Install the IPython kernel spec directory for this Python.

To see all available configurables, use `--help-all`

其他语言的内核

顺便说一下,与这个问题没有严格联系,但有很多其他可用的内核…https://github.com/jupyter/jupyter/wiki/Jupyter-kernels


sudo apt-get install python3-pip python3-dev
pip3 install -U jupyter

用于当前的Python启动器

如果您安装了Py3,但默认为py2

py -3 -m pip install ipykernel
py -3 -m ipykernel install --user

如果您安装了Py2,但默认为py3

py -2 -m pip install ipykernel
py -2 -m ipykernel install --user

当你使用conda管理你的python envs时,遵循以下两个步骤:

激活py3(在Windows上或在Linux上激活py3) Conda install notebook ipykernel或者直接使用Conda install jupyter


以下命令适用于macOS Sierra(10.12.3)

python3 -m pip install ipykernel
python3 -m ipykernel install --user

用 Jupyter内核规格列表 可用内核: python3 /用户/ admin /图书馆/ Jupyter /内核/ python3 python2 / anaconda2 /分享/ jupyter /内核/ python2


我很确定你要做的就是逃跑

Pip3安装jupyter


最短最好的解决方案

conda create --name py3-ml python=3.6
conda install ipykernel

source activate py3-ml # or conda activate py3-ml
python -m ipykernel install --user --name py3-ml

将多个内核安装到单个虚拟环境(venv)

这些答案中的大多数(如果不是全部的话)假设您乐于在全局范围内安装包。这个答案适合你,如果你:

使用*NIX机器 不喜欢全局安装包 不要使用anaconda <->,你很乐意从命令行运行jupyter服务器 想要知道内核安装“在哪里”。

(注意:这个答案在python3-jupyter安装中添加了一个python2内核,但在概念上很容易交换。)

Prerequisites You're in the dir from which you'll run the jupyter server and save files python2 is installed on your machine python3 is installed on your machine virtualenv is installed on your machine Create a python3 venv and install jupyter Create a fresh python3 venv: python3 -m venv .venv Activate the venv: . .venv/bin/activate Install jupyterlab: pip install jupyterlab. This will create locally all the essential infrastructure for running notebooks. Note: by installing jupyterlab here, you also generate default 'kernel specs' (see below) in $PWD/.venv/share/jupyter/kernels/python3/. If you want to install and run jupyter elsewhere, and only use this venv for organizing all your kernels, then you only need: pip install ipykernel You can now run jupyter lab with jupyter lab (and go to your browser to the url displayed in the console). So far, you'll only see one kernel option called 'Python 3'. (This name is determined by the display_name entry in your kernel.json file.) Add a python2 kernel Quit jupyter (or start another shell in the same dir): ctrl-c Deactivate your python3 venv: deactivate Create a new venv in the same dir for python2: virtualenv -p python2 .venv2 Activate your python2 venv: . .venv2/bin/activate Install the ipykernel module: pip install ipykernel. This will also generate default kernel specs for this python2 venv in .venv2/share/jupyter/kernels/python2 Export these kernel specs to your python3 venv: python -m ipykernel install --prefix=$PWD/.venv. This basically just copies the dir $PWD/.venv2/share/jupyter/kernels/python2 to $PWD/.venv/share/jupyter/kernels/ Switch back to your python3 venv and/or rerun/re-examine your jupyter server: deactivate; . .venv/bin/activate; jupyter lab. If all went well, you'll see a Python 2 option in your list of kernels. You can test that they're running real python2/python3 interpreters by their handling of a simple print 'Hellow world' vs print('Hellow world') command. Note: you don't need to create a separate venv for python2 if you're happy to install ipykernel and reference the python2-kernel specs from a global space, but I prefer having all of my dependencies in one local dir

博士TL;

Optionally install an R kernel. This is instructive to develop a sense of what a kernel 'is'. From the same dir, install the R IRkernel package: R -e "install.packages('IRkernel',repos='https://cran.mtu.edu/')". (This will install to your standard R-packages location; for home-brewed-installed R on a Mac, this will look like /usr/local/Cellar/r/3.5.2_2/lib/R/library/IRkernel.) The IRkernel package comes with a function to export its kernel specs, so run: R -e "IRkernel::installspec(prefix=paste(getwd(),'/.venv',sep=''))". If you now look in $PWD/.venv/share/jupyter/kernels/ you'll find an ir directory with kernel.json file that looks something like this:

{
  "argv": ["/usr/local/Cellar/r/3.5.2_2/lib/R/bin/R", "--slave", "-e", "IRkernel::main()", "--args", "{connection_file}"],
  "display_name": "R",
  "language": "R"
}

总之,内核只是从内核调用特定于语言的可执行文件。Json文件,jupyter在…/share/jupyter/kernels目录和列表在本例中,调用R来运行IRkernel::main()函数,该函数将向Jupiter服务器来回发送消息。类似地,python2内核只是使用ipykernel_launcher模块调用python2解释器,如.venv/share/jupyter/kernels/python2/kernel中所示。json等。

如果您想一下子运行所有这些指令,这里有一个脚本。


添加内核意味着你想要使用Jupyter Notebook和列表中没有显示的python版本。

简单的方法-使用所需的python版本启动笔记本,假设我安装了python3.7,然后从终端(cmd)使用以下命令来运行笔记本:

python3.7 -m notebook

有时它会使用别名py, py3.7, python来代替python3.7。


我在python-2中得到了同样的错误。我想运行python-2 jupyter笔记本会话,但默认情况下,我得到了python-3。所以最简单的工作是为python-2打开Anaconda终端并键入“jupyter notebook”,它将毫无问题地启动jupyter-notebook会话。同样,也可以在python-3中尝试


我也面临着同样的问题。每当我使用以下命令启动jupyter notebook时,我都不会看到创建一个新的Python 3 notebook的选项。

我通过运行以下命令解决了这个问题。

pip install notebook

之后,我开始笔记本与相同的命令之前

jupyter notebook

我能够看到我的主目录并创建新的python3笔记本。


这个回答解释了如何使用Poetry依赖管理创建Python 3、Jupyter 1和ipykernel 5工作流。诗歌使创建一个虚拟环境的Jupyter笔记本很容易。我强烈建议不要运行python3命令。安装全局依赖项的Python工作流程会让你陷入依赖项地狱。

以下是对干净、可靠的Poetry工作流程的总结:

安装依赖诗词添加熊猫jupyter ipykernel 在虚拟环境中用诗壳打开一个壳 打开Jupyter notebook,访问与Jupyter notebook相关的所有虚拟环境

还有干净的Conda工作流。注意这个帖子里的很多答案——它们会让你走上一条会给你带来很多痛苦和折磨的道路。


要将特定的python添加到jupyter内核中,首先使用以下命令检查可用的python或python3的路径

$ where python3

假设你有'/usr/local/bin/python3'作为路径之一。要为这个版本的python创建一个名为'name_to_new_kernel'的内核,该内核将显示在jupyter中,

$ /usr/local/bin/python3 -m pip install ipykernel
$ /usr/local/bin/python3 -m ipykernel install --user --name name_to_new_kernel

使用实例检查名称为'name_to_new_kernel'的新内核是否添加到jupyter

jupyter kernelspec list