如何在VSCode中指定ipykernel_launcher的端口?
如何控制VSCode笔记本IPython内核的端口分配?
我使用的是VSCode v1.77.3,它启动IPython内核的命令如下:
/home/username/bin/python3.8 -m ipykernel_launcher --ip=127.0.0.1 --stdin=9010 --control=9006 --hb=9005 --Session.signature_scheme="hmac-sha256" --Session.key=b"72dc9cca-e641-445a-9ceb-3ca5433dc8c0" --shell=9007 --transport="tcp" --iopub=9011 --f=/home/username/.local/share/jupyter/runtime/kernel-v2-3617206EN99MmVys9xf.json
命令里包含四个端口参数:stdin、control、iopub和hb(心跳),默认分配在9000附近。我想知道怎么独立设置每个端口,或者设置一个基准端口让所有端口都大于这个值?
我已经找过VSCode里IPython和Jupyter的相关配置,只找到jupyter.jupyterCommandLineArguments,尝试过以下配置但没有效果:
"jupyter.jupyterCommandLineArguments": [ "--control=55555" ]
VSCode里似乎没有相关文档,想问下能不能通过VSCode配置实现这个需求?
解决方案
目前VSCode的Jupyter扩展并没有直接提供配置IPython内核端口的选项,你尝试的jupyter.jupyterCommandLineArguments是给Jupyter服务器而非内核进程传参的,所以不会生效。
要控制内核的端口分配,可以通过以下两种方式:
1. 独立指定各端口
- 先确认IPython配置目录,默认路径为
~/.ipython/profile_default/,如果不存在,执行ipython profile create命令生成。 - 在该目录下创建或编辑
ipython_kernel_config.py文件,添加以下配置:
替换为你需要的端口数值即可。c.IPKernelApp.stdin_port = 55550 c.IPKernelApp.control_port = 55551 c.IPKernelApp.hb_port = 55552 c.IPKernelApp.iopub_port = 55553 c.IPKernelApp.shell_port = 55554
2. 设置端口范围(基准端口)
如果希望所有端口都大于某个基准值,可在上述配置文件中添加:
c.IPKernelApp.port_range = (55000, 60000)
内核会自动在这个范围内分配可用端口。
修改配置后,重启VSCode和IPython内核即可生效。
内容的提问来源于stack exchange,提问作者farhanhubble
相关产品推荐
相关产品推荐

