如何知道Jupyter笔记本中正在运行哪个?

问题:如何知道Jupyter笔记本中正在运行哪个?

我在用于Python编程的浏览器中使用Jupyter笔记本,已经安装了Anaconda(Python 3.5)。但是我很确定Jupyter使用本地python解释器而不是anaconda运行我的python命令。如何更改它并将Anaconda用作解释器?

Ubuntu 16.10-Anaconda3

I use Jupyter notebook in a browser for Python programming, I have installed Anaconda (Python 3.5). But I’m quite sure that Jupyter in running my python commands with the native python interpreter and not with anaconda. How can I change it and use Anaconda as interpreter?

Ubuntu 16.10 — Anaconda3


回答 0

from platform import python_version

print(python_version())

这将为您提供运行脚本的python的确切版本。例如输出:

3.6.5
from platform import python_version

print(python_version())

This will give you the exact version of python running your script. eg output:

3.6.5

回答 1

import sys
sys.executable

会给你翻译。您可以在创建新笔记本时选择所需的解释器。确保您的anaconda解释器的路径已添加到您的路径中(最有可能在bashrc / bash_profile中的某个位置)。

例如,我以前在.bash_profile中有以下行,我是手动添加的:

export PATH="$HOME/anaconda3/bin:$PATH"

编辑:如评论中所述,这不是将anaconda添加到路径的正确方法。引用Anaconda的文档,应在安装后改为使用以下方法conda init

我应该将Anaconda添加到macOS或Linux PATH吗?

我们不建议手动将Anaconda添加到PATH。在安装过程中,系统将询问您“是否希望安装程序通过运行conda init来初始化Anaconda3?” 我们建议“是”。如果输入“ no”,则conda根本不会修改您的Shell脚本。为了在安装过程完成后进行初始化,请先运行source <path to conda>/bin/activate然后再运行conda init

import sys
sys.executable

will give you the interpreter. You can select the interpreter you want when you create a new notebook. Make sure the path to your anaconda interpreter is added to your path (somewhere in your bashrc/bash_profile most likely).

For example I used to have the following line in my .bash_profile, that I added manually :

export PATH="$HOME/anaconda3/bin:$PATH"

EDIT: As mentioned in a comment, this is not the proper way to add anaconda to the path. Quoting Anaconda’s doc, this should be done instead after install, using conda init:

Should I add Anaconda to the macOS or Linux PATH?

We do not recommend adding Anaconda to the PATH manually. During installation, you will be asked “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”. If you enter “no”, then conda will not modify your shell scripts at all. In order to initialize after the installation process is done, first run source <path to conda>/bin/activate and then run conda init


回答 2

import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)

见下文:-当我在CONDA venv之外运行JupyterNotebook时的输出

/home/dhankar/anaconda2/bin/python
2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
 

当我在使用命令创建的CONDA Venv中运行相同的JupyterNoteBook时看到以下内容-

conda create -n py35 python=3.5 ## Here - py35 , is name of my VENV

在我的Jupyter笔记本中打印:

/home/dhankar/anaconda2/envs/py35/bin/python
3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

另外,如果您已经使用不同版本的Python创建了各种VENV,则可以通过从JupyterNotebook菜单中选择KERNEL >> CHANGE KERNEL切换到所需的内核… JupyterNotebookScreencapture

还要在现有的CONDA虚拟环境中安装ipykernel-

http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments

来源-https ://github.com/jupyter/notebook/issues/1524

 $ /path/to/python -m  ipykernel install --help
 usage: ipython-kernel-install [-h] [--user] [--name NAME]
                          [--display-name DISPLAY_NAME]
                          [--profile PROFILE] [--prefix PREFIX]
                          [--sys-prefix]

安装IPython内核规范。

可选参数:-h,–help显示此帮助消息并退出–user为当前用户而不是系统范围内安装–name NAME指定kernelspec的名称。需要同时具有多个IPython内核。–display-name DISPLAY_NAME指定kernelspec的显示名称。当您有多个IPython内核时,这将很有帮助。–profile PROFILE指定要加载的IPython配置文件。这可以用来创建内核的自定义版本。–prefix PREFIX为kernelspec指定安装前缀。需要将其安装到非默认位置,例如conda / virtual-env。–sys-prefix安装到Python的sys.prefix。–prefix =’/ Users / bussonniermatthias / anaconda’的简写。用于conda / virtual-envs。

 import sys
 print(sys.executable)
 print(sys.version)
 print(sys.version_info)

Seen below :- output when i run JupyterNotebook outside a CONDA venv

/home/dhankar/anaconda2/bin/python
2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)

Seen below when i run same JupyterNoteBook within a CONDA Venv created with command —

conda create -n py35 python=3.5 ## Here - py35 , is name of my VENV

in my Jupyter Notebook it prints :-

/home/dhankar/anaconda2/envs/py35/bin/python
3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

also if you already have various VENV’s created with different versions of Python you switch to the desired Kernel by choosing KERNEL >> CHANGE KERNEL from within the JupyterNotebook menu… JupyterNotebookScreencapture

Also to install ipykernel within an existing CONDA Virtual Environment –

http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments

Source — https://github.com/jupyter/notebook/issues/1524

 $ /path/to/python -m  ipykernel install --help
 usage: ipython-kernel-install [-h] [--user] [--name NAME]
                          [--display-name DISPLAY_NAME]
                          [--profile PROFILE] [--prefix PREFIX]
                          [--sys-prefix]

Install the IPython kernel spec.

optional arguments: -h, –help show this help message and exit –user Install for the current user instead of system-wide –name NAME Specify a name for the kernelspec. This is needed to have multiple IPython kernels at the same time. –display-name DISPLAY_NAME Specify the display name for the kernelspec. This is helpful when you have multiple IPython kernels. –profile PROFILE Specify an IPython profile to load. This can be used to create custom versions of the kernel. –prefix PREFIX Specify an install prefix for the kernelspec. This is needed to install into a non-default location, such as a conda/virtual-env. –sys-prefix Install to Python’s sys.prefix. Shorthand for –prefix=’/Users/bussonniermatthias/anaconda’. For use in conda/virtual-envs.


回答 3

假设您的后端系统错误,则可以kernel通过kernel.jsonkernelsjupyter数据路径的文件夹中创建新的或编辑现有的后端来更改后端jupyter --paths。您可以有多个内核(R,Python2,Python3(+ virtualenvs),Haskell),例如,可以创建一个Anaconda特定的内核:

$ <anaconda-path>/bin/python3 -m ipykernel install --user --name anaconda --display-name "Anaconda"

应该创建一个新内核:

<jupyter-data-dir>/kernels/anaconda/kernel.json

{
    "argv": [ "<anaconda-path>/bin/python3", "-m", "ipykernel", "-f", "{connection_file}" ],
    "display_name": "Anaconda",
    "language": "python"
}

您需要确保ipykernel在anaconda发行版中安装了软件包。

这样,您就可以在内核之间切换,并使用不同的内核使用不同的笔记本。

Assuming you have the wrong backend system you can change the backend kernel by creating a new or editing the existing kernel.json in the kernels folder of your jupyter data path jupyter --paths. You can have multiple kernels (R, Python2, Python3 (+virtualenvs), Haskell), e.g. you can create an Anaconda specific kernel:

$ <anaconda-path>/bin/python3 -m ipykernel install --user --name anaconda --display-name "Anaconda"

Should create a new kernel:

<jupyter-data-dir>/kernels/anaconda/kernel.json

{
    "argv": [ "<anaconda-path>/bin/python3", "-m", "ipykernel", "-f", "{connection_file}" ],
    "display_name": "Anaconda",
    "language": "python"
}

You need to ensure ipykernel package is installed in the anaconda distribution.

This way you can just switch between kernels and have different notebooks using different kernels.


回答 4

为Jupyter Notebook创建虚拟环境

最小的Python安装是

sudo apt install python3.7 python3.7-venv python3.7-minimal python3.7-distutils python3.7-dev python3.7-gdbm python3-gdbm-dbg python3-pip

然后您可以创建和使用环境

/usr/bin/python3.7 -m venv test
cd test
source test/bin/activate
pip install jupyter matplotlib seaborn numpy pandas scipy
# install other packages you need with pip/apt
jupyter notebook
deactivate

您可以使用以下命令为Jupyter创建内核

ipython3 kernel install --user --name=test

Creating a virtual environment for Jupyter Notebooks

A minimal Python install is

sudo apt install python3.7 python3.7-venv python3.7-minimal python3.7-distutils python3.7-dev python3.7-gdbm python3-gdbm-dbg python3-pip

Then you can create and use the environment

/usr/bin/python3.7 -m venv test
cd test
source test/bin/activate
pip install jupyter matplotlib seaborn numpy pandas scipy
# install other packages you need with pip/apt
jupyter notebook
deactivate

You can make a kernel for Jupyter with

ipython3 kernel install --user --name=test