英文:
Install Python package inside a Jupyter Notebook kernel
问题
在Jupyter Notebook中,我已经成功安装了一个Python内核,使用以下命令:
!python -m ipykernel install --user --name other-env --display-name "Python (other-env)"
如这里所述,该内核在菜单Kernel → Change 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虚拟环境
- 从命令行(例如PowerShell)中创建Python虚拟环境,可以参考这里,在一个文件夹中(在本例中是
c:\venvs\butiran
)执行以下命令:
PS C:\> python -m venv c:\venvs\butiran
- 切换到虚拟环境所在的文件夹,例如在本例中是
venvs
文件夹中的butiran
文件夹:
PS C:\> cd venvs\butiran
PS C:\venvs\butiran>
- 激活虚拟环境,可以参考这里的方法:
PS C:\venvs\butiran> Scripts\activate
(butiran) PS C:\venvs\butiran>
- 更新
pip
,可以参考这里的方法:
(butiran) PS C:\venvs\butiran> python -m pip install --upgrade pip
- 安装
ipykernel
,可以参考这里的方法:
(butiran) PS C:\venvs\butiran> pip install ipykernel
- 在PowerShell中退出虚拟环境,可以参考这里的方法:
(butiran) PS C:\venvs\butiran> deactivate
PS C:\venvs\butiran>
Jupyter内核
- 为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>
- 显示已安装内核的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>
- 将Python可执行文件的路径更改为:
- 从
C:\\Users\\Full Name\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe
- 更改为
C:\\venvs\\butiran\\Scripts\\python.exe
- 从
Jupyter Notebook
- 从PowerShell中启动Jupyter Notebook:
PS C:\> jupyter notebook
-
打开现有笔记本或创建一个新的。
-
从菜单 Kernel → Change kernel → 选择 Python (butiran) 更改内核。
-
只在活动内核中安装
a_package
:
%pip install a_package
或者使用以下方式:
import sys
!{sys.executable} -m pip install a_package
- 在全局环境中显示该包:
!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
- 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
- Change directory to the folder, e.g in this case is
butiran
invenvs
(folder for all virtual environments)
PS C:\> cd venvs\butiran
PS C:\venvs\butiran>
- Activate virtual environment as in here
PS C:\venvs\butiran> Scripts\activate
(butiran) PS C:\venvs\butiran>
- Update
pip
as in here
(butiran) PS C:\venvs\butiran> python -m pip install --upgrade pip
- Install
ipykernel
as in here
(butiran) PS C:\venvs\butiran> pip install ipykernel
- Deactivate virtual enviroment in powershell as in here
(butiran) PS C:\venvs\butiran> deactivate
PS C:\venvs\butiran>
Jupyter kernel
- 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>
- 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>
- 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
- Lanuch Jupyter Notebook from PowerShell
PS C:\> jupyter notebook
-
Open existing notebook or create a new one.
-
Change kernel from menu Kernel → Change kernel → Python (butiran).
-
Install
a_package
using only in active kernel
%pip install a_package
or with
import sys
!{sys.executable} -m pip install a_package
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论