使用Python 2。x和Python 3。x在IPython笔记本

时间:2021-08-10 10:19:59

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.

我使用IPython笔记本,并希望能够选择创建一个2。x或3。x python在IPython中的笔记本。

I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and then IPython could be started. This is not what I was looking for so I uninstalled Anaconda and now have set up my own installation using MacPorts and PiP. It seems that I still have to use

我最初有蟒蛇。使用Anaconda,必须更改全局环境变量,以选择您想要的python版本,然后才能启动IPython。这不是我要找的,所以我卸载了Anaconda,现在已经建立了我自己的安装使用MacPorts和PiP。看来我还是要用。

port select --set python <python version> 

to toggle between python 2.x and 3.x. which is no better than the anaconda solution.

在python 2之间切换。倍和3.倍。这不比水蟒溶液好。

Is there a way to select what version of python you want to use after you start an IPython notebook, preferably with my current MacPorts build?

在您启动IPython笔记本后,是否有办法选择您想要使用的python版本,最好是使用我当前的MacPorts构建版本?

10 个解决方案

#1


260  

The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

这里的想法是安装多个ipython内核。这是水蟒的说明。如果您不使用蟒蛇,我最近添加了使用纯virtualenv的指令。

Anaconda 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

自4.1.0版本以来,anaconda包含一个特殊的包nb_conda_kernel,它用笔记本内核检测conda环境并自动注册它们。这使得使用新的python版本和创建新的conda环境一样容易:

conda create -n py27 python=2.7 ipykernel
conda create -n py36 python=3.6 ipykernel

After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new enviroments. The Managing environments section in conda's docs provides further information.

在重新启动jupyter笔记本后,新的内核可以通过图形界面获得。请注意,新包必须显式地安装到新环境中。conda文档中的管理环境部分提供了进一步的信息。

Manually registering kernels

Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

不希望使用nb_conda_kernel或仍然使用旧版本anaconda的用户可以使用以下步骤手工注册ipython内核。

configure the python2.7 environment:

配置python2.7环境:

conda create -n py27 python=2.7
source activate py27
conda install notebook ipykernel
ipython kernel install --user

configure the python3.6 environment:

配置python3.6环境:

conda create -n py36 python=3.6
source activate py36
conda install notebook ipykernel
ipython kernel install --user

After that you should be able to choose between python2
and python3 when creating a new notebook in the interface.

之后,在接口中创建新笔记本时,您应该能够在python2和python3之间进行选择。

Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.

此外,如果您希望更改内核的名称,可以将-name和-display-name选项传递给ipython内核安装。参见ipython内核安装——有关更多信息的帮助。

#2


190  

If you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this:

如果你在Python 3上运行Jupyter,你可以像这样设置一个Python 2内核:

python2 -m pip install ipykernel

python2 -m ipykernel install --user

http://ipython.readthedocs.io/en/stable/install/kernel_install.html

http://ipython.readthedocs.io/en/stable/install/kernel_install.html

#3


34  

With a current version of the Notebook/Jupyter, you can create a Python3 kernel. After starting a new notebook application from the command line with Python 2 you should see an entry „Python 3“ in the dropdown menu „New“. This gives you a notebook that uses Python 3. So you can have two notebooks side-by-side with different Python versions.

使用当前版本的Notebook/Jupyter,您可以创建一个Python3内核。后开始一个新的笔记本从命令行应用程序与Python 2你应该看到一个条目„Python 3”的下拉菜单„新”。这给了你一个使用Python 3的笔记本。因此,您可以在不同的Python版本中并排放置两个笔记本。

The Details

  1. Create this directory: mkdir -p ~/.ipython/kernels/python3
  2. 创建这个目录:mkdir -p ~/.ipython/内核/python3
  3. Create this file ~/.ipython/kernels/python3/kernel.json with this content:

    创建这个文件~ / .ipython /内核/ python3 /内核。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"
        }
    }
    
  4. Restart the notebook server.

    服务器重新启动笔记本电脑。

  5. Select „Python 3“ from the dropdown menu „New“
  6. 从下拉菜单选择„Python 3”„新”
  7. Work with a Python 3 Notebook
  8. 使用Python 3笔记本
  9. Select „Python 2“ from the dropdown menu „New“
  10. 从下拉菜单选择„Python 2”„新”
  11. Work with a Python 2 Notebook
  12. 使用Python 2笔记本

#4


27  

These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.

这些指令解释了如何在独立的虚拟环境中为非anaconda用户安装python2和python3内核。如果您正在使用蟒蛇,请找到我的另一个解决方案直接适合蟒蛇。

I assume that you already have jupyter notebook installed.

我想你已经安装了jupyter笔记本。


First make sure that you have a python2 and a python3 interpreter with pip available.

首先,确保您有一个带有pip的python2和python3解释器。

On ubuntu you would install these by:

在ubuntu上,你可以通过以下方式安装:

sudo apt-get install python-dev python3-dev python-pip python3-pip

Next prepare and register the kernel environments

接下来准备并注册内核环境

python -m pip install virtualenv --user

# configure python2 kernel
python -m virtualenv -p python2 ~/py2_kernel
source ~/py2_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py2 --user
deactivate

# configure python3 kernel
python -m virtualenv -p python3 ~/py3_kernel
source ~/py3_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py3 --user
deactivate

To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc

为了使事情更简单,您可能想要将激活命令的shell别名添加到shell配置文件中。根据你使用的系统和外壳,这可以是~/。bashrc,(~ /。bash_profile或~ / . zshrc

alias kernel2='source ~/py2_kernel/bin/activate'
alias kernel3='source ~/py3_kernel/bin/activate'

After restarting your shell, you can now install new packages after activating the environment you want to use.

在重新启动shell之后,您现在可以在激活您想要使用的环境之后安装新的包。

kernel2
python -m pip install <pkg-name>
deactivate

or

kernel3
python -m pip install <pkg-name>
deactivate

#5


20  

A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.

有一种解决方案允许我通过配置Ipython内核规范来保持MacPorts安装。

Requirements:

要求:

  • MacPorts is installed in the usual /opt directory
  • MacPorts安装在通常的/opt目录中
  • python 2.7 is installed through macports
  • python 2.7是通过macports安装的
  • python 3.4 is installed through macports
  • python 3.4是通过macports安装的
  • Ipython is installed for python 2.7
  • 为python 2.7安装了Ipython
  • Ipython is installed for python 3.4
  • 为python 3.4安装了Ipython

For python 2.x:

对于python 2. x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self

For python 3.x:

对于python 3. x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self

Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.

现在,您可以打开一个Ipython笔记本,然后选择一个python 2。x或python 3。x笔记本。

使用Python 2。x和Python 3。x在IPython笔记本

#6


16  

From my Linux installation I did:

从我的Linux安装,我做了:

sudo ipython2 kernelspec install-self

sudo ipython2 kernelspec install-self

And now my python 2 is back on the list.

现在我的python 2回到列表中。

Reference:

参考:

http://ipython.readthedocs.org/en/latest/install/kernel_install.html

http://ipython.readthedocs.org/en/latest/install/kernel_install.html


UPDATE:

更新:

The method above is now deprecated and will be dropped in the future. The new method should be:

上面的方法现在已被弃用,将来将被删除。新的方法应该是:

sudo ipython2 kernel install

sudo ipython2内核安装

#7


2  

Following are the steps to add the python2 kernel to jupyter notebook::

以下是将python2内核添加到jupyter记事本的步骤:

open a terminal and create a new python 2 environment: conda create -n py27 python=2.7

打开一个终端,创建一个新的python 2环境:conda创建-n py27 python=2.7。

activate the environment: Linux source activate py27 or windows activate py27

激活环境:Linux源代码激活py27或windows激活py27

install the kernel in the env: conda install notebook ipykernel

在env中安装内核:conda安装笔记本ipykernel

install the kernel for outside the env: ipython kernel install --user

为env: ipython内核安装——用户安装外部安装内核

close the env: source deactivate

关闭env:源禁用

Although a late answer hope someone finds it useful :p

虽然一个迟来的答案希望有人发现它有用:p

#8


1  

I looked at this excellent info and then wondered, since

我看了这个极好的信息,然后想,从那以后

  1. i have python2, python3 and IPython all installed,
  2. 我已经安装了python2, python3和IPython,
  3. i have PyCharm installed,
  4. 我安装了PyCharm,
  5. PyCharm uses IPython for its Python Console,
  6. PyCharm使用IPython作为Python控制台,

if PyCharm would use

如果PyCharm会使用

  1. IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
  2. 当菜单>文件>设置>项目>项目解释器= py2时
  3. IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3
  4. 当菜单>文件>设置>项目>项目解释器= py3时

ANSWER: Yes!

答:是的!

P.S. i have Python Launcher for Windows installed as well.

另外,我还安装了用于Windows的Python启动程序。

#9


1  

Use sudo pip3 install jupyter for installing jupyter for python3 and sudo pip install jupyter for installing jupyter notebook for python2. Then, you can call ipython kernel install command to enable both types of notebook to choose from in jupyter notebook.

使用sudo pip3安装jupyter为python3安装jupyter,而sudo pip安装jupyter为python2安装jupyter。然后,您可以调用ipython内核安装命令,使两种类型的笔记本都可以在jupyter笔记本中进行选择。

#10


0  

Under Windows 7 I had anaconda and anaconda3 installed. I went into \Users\me\anaconda\Scripts and executed

在Windows 7下,我安装了anaconda和anaconda3。我进入\用户\anaconda\脚本并执行

sudo .\ipython kernelspec install-self

then I went into \Users\me\anaconda3\Scripts and executed

然后,我进入了\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \执行。

sudo .\ipython kernel install

(I got jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.)

(我得到了jupyter kernelspec安装-self在4.0中被弃用。您可能希望“ipython内核安装”来安装ipython内核规范。

After starting jupyter notebook (in anaconda3) I got a neat dropdown menu in the upper right corner under "New" letting me choose between Python 2 odr Python 3 kernels.

在启动jupyter笔记本(在anaconda3)后,我在右上角有一个整洁的下拉菜单,在“New”让我在Python 2 odr Python 3内核中选择。

#1


260  

The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

这里的想法是安装多个ipython内核。这是水蟒的说明。如果您不使用蟒蛇,我最近添加了使用纯virtualenv的指令。

Anaconda 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

自4.1.0版本以来,anaconda包含一个特殊的包nb_conda_kernel,它用笔记本内核检测conda环境并自动注册它们。这使得使用新的python版本和创建新的conda环境一样容易:

conda create -n py27 python=2.7 ipykernel
conda create -n py36 python=3.6 ipykernel

After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new enviroments. The Managing environments section in conda's docs provides further information.

在重新启动jupyter笔记本后,新的内核可以通过图形界面获得。请注意,新包必须显式地安装到新环境中。conda文档中的管理环境部分提供了进一步的信息。

Manually registering kernels

Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

不希望使用nb_conda_kernel或仍然使用旧版本anaconda的用户可以使用以下步骤手工注册ipython内核。

configure the python2.7 environment:

配置python2.7环境:

conda create -n py27 python=2.7
source activate py27
conda install notebook ipykernel
ipython kernel install --user

configure the python3.6 environment:

配置python3.6环境:

conda create -n py36 python=3.6
source activate py36
conda install notebook ipykernel
ipython kernel install --user

After that you should be able to choose between python2
and python3 when creating a new notebook in the interface.

之后,在接口中创建新笔记本时,您应该能够在python2和python3之间进行选择。

Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.

此外,如果您希望更改内核的名称,可以将-name和-display-name选项传递给ipython内核安装。参见ipython内核安装——有关更多信息的帮助。

#2


190  

If you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this:

如果你在Python 3上运行Jupyter,你可以像这样设置一个Python 2内核:

python2 -m pip install ipykernel

python2 -m ipykernel install --user

http://ipython.readthedocs.io/en/stable/install/kernel_install.html

http://ipython.readthedocs.io/en/stable/install/kernel_install.html

#3


34  

With a current version of the Notebook/Jupyter, you can create a Python3 kernel. After starting a new notebook application from the command line with Python 2 you should see an entry „Python 3“ in the dropdown menu „New“. This gives you a notebook that uses Python 3. So you can have two notebooks side-by-side with different Python versions.

使用当前版本的Notebook/Jupyter,您可以创建一个Python3内核。后开始一个新的笔记本从命令行应用程序与Python 2你应该看到一个条目„Python 3”的下拉菜单„新”。这给了你一个使用Python 3的笔记本。因此,您可以在不同的Python版本中并排放置两个笔记本。

The Details

  1. Create this directory: mkdir -p ~/.ipython/kernels/python3
  2. 创建这个目录:mkdir -p ~/.ipython/内核/python3
  3. Create this file ~/.ipython/kernels/python3/kernel.json with this content:

    创建这个文件~ / .ipython /内核/ python3 /内核。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"
        }
    }
    
  4. Restart the notebook server.

    服务器重新启动笔记本电脑。

  5. Select „Python 3“ from the dropdown menu „New“
  6. 从下拉菜单选择„Python 3”„新”
  7. Work with a Python 3 Notebook
  8. 使用Python 3笔记本
  9. Select „Python 2“ from the dropdown menu „New“
  10. 从下拉菜单选择„Python 2”„新”
  11. Work with a Python 2 Notebook
  12. 使用Python 2笔记本

#4


27  

These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.

这些指令解释了如何在独立的虚拟环境中为非anaconda用户安装python2和python3内核。如果您正在使用蟒蛇,请找到我的另一个解决方案直接适合蟒蛇。

I assume that you already have jupyter notebook installed.

我想你已经安装了jupyter笔记本。


First make sure that you have a python2 and a python3 interpreter with pip available.

首先,确保您有一个带有pip的python2和python3解释器。

On ubuntu you would install these by:

在ubuntu上,你可以通过以下方式安装:

sudo apt-get install python-dev python3-dev python-pip python3-pip

Next prepare and register the kernel environments

接下来准备并注册内核环境

python -m pip install virtualenv --user

# configure python2 kernel
python -m virtualenv -p python2 ~/py2_kernel
source ~/py2_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py2 --user
deactivate

# configure python3 kernel
python -m virtualenv -p python3 ~/py3_kernel
source ~/py3_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py3 --user
deactivate

To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc

为了使事情更简单,您可能想要将激活命令的shell别名添加到shell配置文件中。根据你使用的系统和外壳,这可以是~/。bashrc,(~ /。bash_profile或~ / . zshrc

alias kernel2='source ~/py2_kernel/bin/activate'
alias kernel3='source ~/py3_kernel/bin/activate'

After restarting your shell, you can now install new packages after activating the environment you want to use.

在重新启动shell之后,您现在可以在激活您想要使用的环境之后安装新的包。

kernel2
python -m pip install <pkg-name>
deactivate

or

kernel3
python -m pip install <pkg-name>
deactivate

#5


20  

A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.

有一种解决方案允许我通过配置Ipython内核规范来保持MacPorts安装。

Requirements:

要求:

  • MacPorts is installed in the usual /opt directory
  • MacPorts安装在通常的/opt目录中
  • python 2.7 is installed through macports
  • python 2.7是通过macports安装的
  • python 3.4 is installed through macports
  • python 3.4是通过macports安装的
  • Ipython is installed for python 2.7
  • 为python 2.7安装了Ipython
  • Ipython is installed for python 3.4
  • 为python 3.4安装了Ipython

For python 2.x:

对于python 2. x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self

For python 3.x:

对于python 3. x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self

Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.

现在,您可以打开一个Ipython笔记本,然后选择一个python 2。x或python 3。x笔记本。

使用Python 2。x和Python 3。x在IPython笔记本

#6


16  

From my Linux installation I did:

从我的Linux安装,我做了:

sudo ipython2 kernelspec install-self

sudo ipython2 kernelspec install-self

And now my python 2 is back on the list.

现在我的python 2回到列表中。

Reference:

参考:

http://ipython.readthedocs.org/en/latest/install/kernel_install.html

http://ipython.readthedocs.org/en/latest/install/kernel_install.html


UPDATE:

更新:

The method above is now deprecated and will be dropped in the future. The new method should be:

上面的方法现在已被弃用,将来将被删除。新的方法应该是:

sudo ipython2 kernel install

sudo ipython2内核安装

#7


2  

Following are the steps to add the python2 kernel to jupyter notebook::

以下是将python2内核添加到jupyter记事本的步骤:

open a terminal and create a new python 2 environment: conda create -n py27 python=2.7

打开一个终端,创建一个新的python 2环境:conda创建-n py27 python=2.7。

activate the environment: Linux source activate py27 or windows activate py27

激活环境:Linux源代码激活py27或windows激活py27

install the kernel in the env: conda install notebook ipykernel

在env中安装内核:conda安装笔记本ipykernel

install the kernel for outside the env: ipython kernel install --user

为env: ipython内核安装——用户安装外部安装内核

close the env: source deactivate

关闭env:源禁用

Although a late answer hope someone finds it useful :p

虽然一个迟来的答案希望有人发现它有用:p

#8


1  

I looked at this excellent info and then wondered, since

我看了这个极好的信息,然后想,从那以后

  1. i have python2, python3 and IPython all installed,
  2. 我已经安装了python2, python3和IPython,
  3. i have PyCharm installed,
  4. 我安装了PyCharm,
  5. PyCharm uses IPython for its Python Console,
  6. PyCharm使用IPython作为Python控制台,

if PyCharm would use

如果PyCharm会使用

  1. IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
  2. 当菜单>文件>设置>项目>项目解释器= py2时
  3. IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3
  4. 当菜单>文件>设置>项目>项目解释器= py3时

ANSWER: Yes!

答:是的!

P.S. i have Python Launcher for Windows installed as well.

另外,我还安装了用于Windows的Python启动程序。

#9


1  

Use sudo pip3 install jupyter for installing jupyter for python3 and sudo pip install jupyter for installing jupyter notebook for python2. Then, you can call ipython kernel install command to enable both types of notebook to choose from in jupyter notebook.

使用sudo pip3安装jupyter为python3安装jupyter,而sudo pip安装jupyter为python2安装jupyter。然后,您可以调用ipython内核安装命令,使两种类型的笔记本都可以在jupyter笔记本中进行选择。

#10


0  

Under Windows 7 I had anaconda and anaconda3 installed. I went into \Users\me\anaconda\Scripts and executed

在Windows 7下,我安装了anaconda和anaconda3。我进入\用户\anaconda\脚本并执行

sudo .\ipython kernelspec install-self

then I went into \Users\me\anaconda3\Scripts and executed

然后,我进入了\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \执行。

sudo .\ipython kernel install

(I got jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.)

(我得到了jupyter kernelspec安装-self在4.0中被弃用。您可能希望“ipython内核安装”来安装ipython内核规范。

After starting jupyter notebook (in anaconda3) I got a neat dropdown menu in the upper right corner under "New" letting me choose between Python 2 odr Python 3 kernels.

在启动jupyter笔记本(在anaconda3)后,我在右上角有一个整洁的下拉菜单,在“New”让我在Python 2 odr Python 3内核中选择。