在 IPython Notebook 中同时使用 Python 2.x 和 Python 3.x

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30492623/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:31:47  来源:igfitidea点击:

Using both Python 2.x and Python 3.x in IPython Notebook

pythonpython-2.7python-3.xipythonipython-notebook

提问by deltap

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 笔记本,并希望能够选择在 IPython 中创建 2.x 或 3.x python 笔记本。

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。使用 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.x 和 3.x 之间切换。这并不比 anaconda 解决方案好。

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 版本?

采纳答案by deltap

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
  • python 2.7 is installed through macports
  • python 3.4 is installed through macports
  • Ipython is installed for python 2.7
  • Ipython is installed for python 3.4
  • MacPorts 安装在通常的 /opt 目录中
  • python 2.7是通过macports安装的
  • python 3.4通过macports安装
  • 为 python 2.7 安装了 Ipython
  • 为 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 笔记本。

Choose your python!

选择你的蟒蛇!

回答by cel

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

这里的想法是安装多个ipython内核。以下是 anaconda 的说明。如果你不使用蟒蛇,我最近添加的说明采用纯virtualenvs。

Anaconda >= 4.1.0

蟒蛇 >= 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernelsthat 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_kernels,它可以检测带有笔记本内核的 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 environments. The Managing environmentssection in conda's docs provides further information.

重新启动 jupyter notebook 后,新内核可通过图形界面使用。请注意,必须将新软件包显式安装到新环境中。conda 文档中的管理环境部分提供了更多信息。

Manually registering kernels

手动注册内核

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

不想使用nb_conda_kernels或仍然使用旧版本anaconda的用户可以使用以下步骤手动注册ipython内核。

configure the python2.7environment:

配置python2.7环境:

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

configure the python3.6environment:

配置python3.6环境:

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

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

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

Additionally you can pass the --nameand --display-nameoptions to ipython kernel installif you want to change the names of your kernels. See ipython kernel install --helpfor more informations.

此外,如果您想更改内核的名称,您可以将--name--display-name选项传递给ipython kernel install。有关ipython kernel install --help更多信息,请参阅。

回答by Mike Müller

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. Create this file ~/.ipython/kernels/python3/kernel.jsonwith this content:

    {
        "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"
        }
    }
    
  3. Restart the notebook server.

  4. Select ?Python 3“ from the dropdown menu ?New“
  5. Work with a Python 3 Notebook
  6. Select ?Python 2“ from the dropdown menu ?New“
  7. Work with a Python 2 Notebook
  1. 创建这个目录: mkdir -p ~/.ipython/kernels/python3
  2. ~/.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"
        }
    }
    
  3. 重新启动笔记本服务器。

  4. 从下拉菜单“新建”中选择“Python 3”
  5. 使用 Python 3 Notebook
  6. 从下拉菜单中选择“Python 2”“新建”
  7. 使用 Python 2 Notebook

回答by mimoralea

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 kernel install

回答by cel

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 answerfor a solution directly tailored to anaconda.

这些说明解释了如何为非 anaconda 用户在单独的虚拟环境中安装 python2 和 python3 内核。如果您正在使用 anaconda,请找到我的其他答案,以获得直接为 anaconda 量身定制的解决方案。

I assume that you already have jupyter notebookinstalled.

我假设你已经jupyter notebook安装了。



First make sure that you have a python2and a python3interpreter with pipavailable.

首先确保您有一个可用python2python3口译员pip

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_profileor ~/.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

回答by Love and peace - Joe Codeswell

I looked at this excellent info and then wondered, since

我看了这个极好的信息然后想知道,因为

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

ifPyCharmwould use

如果PyCharm会使用

  1. IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
  2. IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3
  1. IPython-py2 当 Menu>File>Settings>Project>Project Interpreter == py2 AND
  2. IPython-py3 当 Menu>File>Settings>Project>Project Interpreter == py3

ANSWER: Yes!

答:是的!

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

PS 我也安装了适用于 Windows 的 Python Launcher。

回答by Ish

If you're running Jupyteron 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

回答by Krischu

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

在 Windows 7 下,我安装了 anaconda 和 anaconda3。我进入\Users\me\anaconda\Scripts并执行

sudo .\ipython kernelspec install-self

then I went into \Users\me\anaconda3\Scriptsand executed

然后我进入\Users\me\anaconda3\Scripts并执行

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 install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.

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 notebook(在 anaconda3 中),我在右上角的“New”下有一个整洁的下拉菜单,让我可以在 Python 2 odr Python 3 内核之间进行选择。

回答by sv_jan5

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

使用sudo pip3 install jupyter安装了python3 jupyter和sudo pip install jupyter安装jupyter笔记本python2。然后,您可以调用ipython kernel install命令在 jupyter notebook 中启用两种类型的 notebook 进行选择。

回答by Akash Chandra

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

以下是将python2内核添加到jupyter notebook的步骤:

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

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

activate the environment: Linux source activate py27or windows activate py27

激活环境:Linuxsource activate py27或 windowsactivate py27

install the kernel in the env: conda install notebook ipykernel

在 env 中安装内核: conda install notebook ipykernel

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

在 env 之外安装内核: ipython kernel install --user

close the env: source deactivate

关闭环境: source deactivate

Although a late answer hope someone finds it useful :p

虽然迟到的答案希望有人觉得它有用:p