Python 如何知道哪个正在 Jupyter notebook 中运行?

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

How to know which is running in Jupyter notebook ?

pythonanacondajupyter-notebookjupyter

提问by Victor

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 ?

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

Thanks!

谢谢!

Ubuntu 16.10 -- Anaconda3

Ubuntu 16.10——Anaconda3

回答by Davies Odu

from platform import python_version

print(python_version())

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

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

3.6.5

回答by P. Camilleri

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).

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

For example I have the following line in my .bash_profile :

例如,我的 .bash_profile 中有以下行:

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

回答by Rohit Dhankar

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

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

如下所示:- 当我在 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)

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

当我在使用命令创建的 CONDA Venv 中运行相同的 JupyterNoteBook 时,如下所示 -

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

in my Jupyter Notebook it prints :-

在我的 Jupyter Notebook 中,它打印:-

/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

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

Also to install ipykernel within an existing CONDA Virtual Environment -

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

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

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

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

来源 --- 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.

安装 IPython 内核规范。

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.

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

回答by AChampion

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

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

$ <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

<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 ipykernelpackage is installed in the anaconda distribution.

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

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

通过这种方式,您可以在内核之间切换,并使用不同的内核拥有不同的笔记本。

回答by Daisuke Aramaki

Creating a virtual environment for Jupyter Notebooks

为 Jupyter Notebooks 创建虚拟环境

A minimal Python install is

最小的 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

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

你可以为 Jupyter 制作一个内核

ipython3 kernel install --user --name=test