在 Jupyter Notebook 内核中安装 Python 包

huangapple go评论53阅读模式
英文:

Install Python package inside a Jupyter Notebook kernel

问题

在Jupyter Notebook中,我已经成功安装了一个Python内核,使用以下命令:

!python -m ipykernel install --user --name other-env --display-name "Python (other-env)"

这里所述,该内核在菜单KernelChange kernel中与其他内核一起可用,并且在执行以下命令时也会显示:

!jupyter kernelspec list

它将显示如下:

Available kernels:
  avenv         C:\Users\Full Name\AppData\Roaming\jupyter\kernels\avenv
  chatterbot    C:\Users\Full Name\AppData\Roaming\jupyter\kernels\chatterbot
  othervenv     C:\Users\Full Name\AppData\Roaming\jupyter\kernels\othervenv
  python3       C:\Users\Full Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\share\jupyter\kernels\python3

然后,我尝试使用以下命令来安装Python包:

%pip install a_package

根据这里的说明:

使用%(而不是!),它将安装a_package到当前内核中(而不是安装到启动笔记本的Python实例中)。

但我发现它会将a_package安装到所有内核中,或者%pip list将在所有内核中列出相同的已安装包。

是否有办法只将Python包安装到活动的Jupyter Notebook内核中?

英文:

Inside a Jupyter Notebook I have managed to install a Python kernel with

!python -m ipykernel install --user --name other-env --display-name "Python (other-env)"

as informed here and it is available with other kernels on the menu Kernel → Change kernel and

!jupyter kernelspec list

will also show them

Available kernels:
  avenv         C:\Users\Full Name\AppData\Roaming\jupyter\kernels\avenv
  chatterbot    C:\Users\Full Name\AppData\Roaming\jupyter\kernels\chatterbot
  othervenv     C:\Users\Full Name\AppData\Roaming\jupyter\kernels\othervenv
  python3       C:\Users\Full Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\share\jupyter\kernels\python3

Then I try to install Python a package using

%pip install a_package

as given here that said

> with % (instead of !) it will install a_package into the current kernel (rather than into the instance of Python that launched the notebook).

But what I have got that it installs a_package to all kernels, or %pip list will list the same installed packages in all kernels.

Is there a way to install Python package only to an active Jupyter Notebook kernel?

答案1

得分: 1

以下是我用来解决这个问题的代码部分:

import sys
!{sys.executable} -m pip install a_package
英文:

Here is what I have used to overcome this issue

import sys
!{sys.executable} -m pip install a_package

答案2

得分: 1

简短回答

Jupyter内核的JSON文件中的Python可执行文件路径应该指向相关虚拟环境中的Python可执行文件。

详细回答

经过一些尝试,这些步骤可以总结如下。

Python虚拟环境

  1. 从命令行(例如PowerShell)中创建Python虚拟环境,可以参考这里,在一个文件夹中(在本例中是c:\venvs\butiran)执行以下命令:
PS C:\> python -m venv c:\venvs\butiran
  1. 切换到虚拟环境所在的文件夹,例如在本例中是venvs文件夹中的butiran文件夹:
PS C:\> cd venvs\butiran
PS C:\venvs\butiran>
  1. 激活虚拟环境,可以参考这里的方法:
PS C:\venvs\butiran> Scripts\activate
(butiran) PS C:\venvs\butiran>
  1. 更新pip,可以参考这里的方法:
(butiran) PS C:\venvs\butiran> python -m pip install --upgrade pip
  1. 安装ipykernel,可以参考这里的方法:
(butiran) PS C:\venvs\butiran> pip install ipykernel
  1. 在PowerShell中退出虚拟环境,可以参考这里的方法:
(butiran) PS C:\venvs\butiran> deactivate
PS C:\venvs\butiran>

Jupyter内核

  1. 为Jupyter Notebook安装ipykernel,可以参考这里的方法:
PS C:\venvs\butiran> python -m ipykernel install --user --name butiran --display-name "Python (butiran)"
Installed kernelspec butiran in C:\Users\Full Name\AppData\Roaming\jupyter\kernels\butiran
PS C:\venvs\butiran>
  1. 显示已安装内核的JSON文件:
PS C:\venvs\butiran> cat "C:\Users\Full Name\AppData\Roaming\jupyter\kernels\butiran\kernel.json"
{
 "argv": [
  "C:\\Users\\Full Name\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python (butiran)",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}
PS C:\venvs\butiran>
  1. 将Python可执行文件的路径更改为:
    • C:\\Users\\Full Name\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe
    • 更改为 C:\\venvs\\butiran\\Scripts\\python.exe

Jupyter Notebook

  1. 从PowerShell中启动Jupyter Notebook:
PS C:\> jupyter notebook
  1. 打开现有笔记本或创建一个新的。

  2. 从菜单 Kernel → Change kernel → 选择 Python (butiran) 更改内核。

  3. 只在活动内核中安装a_package

%pip install a_package

或者使用以下方式:

import sys
!{sys.executable} -m pip install a_package
  1. 在全局环境中显示该包:
!pip show a_package

将显示:

WARNING: Package(s) not found: a_package

而在活动内核中显示该包:

%pip show a_package

将显示:

Name: a_package
Version: ..
Summary: ..
..
Location: c:\venvs\butiran\lib\site-packages
英文:

Short answer

Path of python executable in JSON file of Jupyter kernel should point to python executable in related virtual environment.

Long answer

After some tries the steps can be summarized as follow.

Python virtual environment

  1. Create Python virtual environment from a shell, e.g. PowerShell, as in here, in a folder (in this case it is c:\venvs\butiran)
PS C:\> python -m venv c:\venvs\butiran
  1. Change directory to the folder, e.g in this case is butiran in venvs (folder for all virtual environments)
PS C:\> cd venvs\butiran
PS C:\venvs\butiran>
  1. Activate virtual environment as in here
PS C:\venvs\butiran> Scripts\activate
(butiran) PS C:\venvs\butiran>
  1. Update pip as in here
(butiran) PS C:\venvs\butiran> python -m pip install --upgrade pip
  1. Install ipykernel as in here
(butiran) PS C:\venvs\butiran> pip install ipykernel
  1. Deactivate virtual enviroment in powershell as in here
(butiran) PS C:\venvs\butiran> deactivate
PS C:\venvs\butiran>

Jupyter kernel

  1. Install ipykernel for Jupyter Notebook as in here
PS C:\venvs\butiran> python -m ipykernel install --user --name butiran --display-name "Python (butiran)"
Installed kernelspec butiran in C:\Users\Full Name\AppData\Roaming\jupyter\kernels\butiran
PS C:\venvs\butiran>
  1. Show JSON file of installed kernel
PS C:\venvs\butiran> cat "C:\Users\Full Name\AppData\Roaming\jupyter\kernels\butiran\kernel.json"
{
 "argv": [
  "C:\\Users\\Full Name\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python (butiran)",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}
PS C:\venvs\butiran>
  1. Change path to python.exe
  • from C:\\Users\\Full Name\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe
  • to C:\\venvs\\butiran\\Scripts\\python.exe

Jupyter Notebook

  1. Lanuch Jupyter Notebook from PowerShell
PS C:\> jupyter notebook
  1. Open existing notebook or create a new one.

  2. Change kernel from menu Kernel → Change kernel → Python (butiran).

  3. Install a_package using only in active kernel

%pip install a_package

or with

import sys
!{sys.executable} -m pip install a_package
  1. Show the package globally
!pip show a_package

will give

WARNING: Package(s) not found: a_package

while show the package in active kernel

%pip show a_package

will give

Name: a_package
Version: ..
Summary: ..
..
Location: c:\venvs\butiran\lib\site-packages

huangapple
  • 本文由 发表于 2023年2月8日 23:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定