Skip to content

Commit 188bf50

Browse files
ci: update file from GitHub Action [skip ci]
1 parent e8f4633 commit 188bf50

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

docs/en/solutions/How_to_add_ipykernel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ kind:
55
- Solution
66
ProductsVersion:
77
- 1.5
8+
id: KB260200003
89
---
910
# Add a New ipykernel in JupyterLab
1011

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
products:
3+
- Alauda AI
4+
kind:
5+
- Solution
6+
ProductsVersion:
7+
- 1.5
8+
id: KB260200003
9+
sourceSHA: 9c12e214a59bf15e3394d346dc38c890a79c375be3e9b82856d21267ce1859d0
10+
---
11+
12+
# 在 JupyterLab 中添加新的 ipykernel
13+
14+
本文档解释了如何创建一个新的 Python 虚拟环境,将其注册为 Jupyter ipykernel,并验证它在 JupyterLab 中是否可用。
15+
16+
本指南中的所有命令应在 **JupyterLab 环境** 内执行。您可以使用 Kubernetes 命令进入 JupyterLab Pod,或直接从 JupyterLab 启动页面上的 **终端** 运行命令。
17+
18+
## 下载额外的 Python 版本(可选)
19+
20+
JupyterLab 中内置的 Python 版本是 **Python 3.11**。如果您需要基于不同 Python 版本的虚拟环境,建议下载 **预构建的独立 Python 发行版**,而不是手动编译 Python 或安装系统软件包。
21+
22+
在本指南中,我们使用 **python-build-standalone**,它提供了一个 *干净、无依赖、预编译的 Linux Python 二进制文件*。这种方法不依赖于系统库,不需要 root 权限。非常适合容器化或受限环境,如 JupyterLab。
23+
24+
以下示例演示如何使用 python-build-standalone 版本安装 **Python 3.10**
25+
26+
```bash
27+
# 创建一个目录
28+
mkdir -p ~/python310_static && cd ~/python310_static
29+
30+
# 下载预构建的 Python 3.10 压缩包(最近的 2024 构建)
31+
curl -L https://github.com/indygreg/python-build-standalone/releases/download/20240107/cpython-3.10.13+20240107-x86_64-unknown-linux-gnu-install_only.tar.gz | tar -xz
32+
33+
# 验证 Python 版本(预期输出:Python 3.10.13)
34+
~/python310_static/python/bin/python3 --version
35+
36+
# 使用 Python 3.10 创建虚拟环境
37+
~/python310_static/python/bin/python3 -m venv ~/.venv-py310
38+
```
39+
40+
## 创建并注册新的 ipykernel
41+
42+
首先,创建一个新的 Python 虚拟环境:
43+
44+
```bash
45+
python -m venv ~/.venv-testing
46+
47+
# 如果您在前一部分安装了 Python 3.10,请使用:
48+
~/python310_static/python/bin/python3 -m venv ~/.venv-py310
49+
```
50+
51+
激活虚拟环境:
52+
53+
```bash
54+
source ~/.venv-testing/bin/activate
55+
56+
# 如果您使用的是 Python 3.10 虚拟环境:
57+
source ~/.venv-py310/bin/activate
58+
```
59+
60+
在虚拟环境中安装所需的 `ipykernel` 包:
61+
62+
```bash
63+
pip install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple
64+
```
65+
66+
将此虚拟环境注册为 Jupyter 内核:
67+
68+
```bash
69+
python -m ipykernel install \
70+
--user \
71+
--name python-testing \
72+
--display-name "Python (testing)"
73+
```
74+
75+
验证新内核是否已成功注册:
76+
77+
```bash
78+
jupyter kernelspec list
79+
```
80+
81+
示例输出:
82+
83+
```text
84+
可用内核:
85+
python3 /home/jovyan/.venv/share/jupyter/kernels/python
86+
python-testing /home/jovyan/.local/share/jupyter/kernels/python-testing
87+
```
88+
89+
## 在 JupyterLab 中验证内核
90+
91+
注册内核后,请在浏览器中刷新 JupyterLab 页面。
92+
93+
**启动器** 页面上,**Notebook****控制台** 部分下将出现一个名为 **Python (testing)** 的新卡片。
94+
95+
点击 **控制台** 下的 **Python (testing)** 以打开一个新的控制台标签,然后运行以下代码以验证内核是否使用正确的环境:
96+
97+
```python
98+
import sys
99+
100+
print("Python version:", sys.version)
101+
print("Python executable:", sys.executable)
102+
```
103+
104+
如果输出显示 Python 可执行文件路径指向新创建的虚拟环境,则 ipykernel 配置正确。例如:
105+
106+
```text
107+
Python version: 3.11.13 (main, ...)
108+
Python executable: /home/jovyan/.venv-testing/bin/python
109+
```

0 commit comments

Comments
 (0)