Sublime Text 安装 Helium 插件搭出Jupyter Notebook的效果
自从用Python编程以来,使用过几个编辑器:Jupyter Notebook、Spyder、PyCharm、VS Code、Sublime Text。总的来说,尺短寸长,各有千秋。
课堂教学的话,首选Jupyter Notebook,Jupyter Notebook强大的交互功能对教学特别友好,可以分段运行,直接看到代码段的运行结果,图文并茂。但是其独特的.ipynb文件格式不能直接在其他编辑器环境下运行。
Spyder是Anaconda安装时自带的编辑器,PyCharm是第三方IDE,对于项目环境管理比较友好,可以方便地切换python解释器。VS Code现在比较火,集成了多种编程语言,安装包也比较方便。做项目的话,这三个IDE都还不错。但是这三者有一个共同点,就是启动太慢,准备工作要很久。
相比之下,Sublime Text简洁的环境和秒开的速度深得我心。稍微有些麻烦的是各种插件的配置。下面就对最近倒腾Sublime Text的几个问题做个总结,以免遗忘。
1 SublimeText4安装
之前一直在用Sublime Text3,前段时间不知咋回事,成了未注册版本。新出了4之后,果断换新版本。官网下载安装http://www.sublimetext.com/。目前 Version: Build 4113。
购买或网上搜索破解方法,这里就不多说了。
2 常见问题解决
问题:运行python文件报错:
__init__() got an unexpected keyword argument 'kill_previous'
解决办法:
Preferences首选项→浏览插件目录,删除默认的Default目录
重启 Sublime Text
问题:python代码运行后无法在控制台使用input函数输入
解决办法:
安装插件SublimeREPL
Ctrl+shift+p ->在弹出出口中输入REPL->找到SublimeREPL
安装成功后,
①可以安装如下方式运行python文件:
Tools->SublimeREPL->Python->Python-RUN current file
②创建快捷键
Preferences首选项- Key Bindings热键绑定,点击之后会弹出两个界面:default 和 user
分别在两个界面输入以下内容,设置Ctrl+m快捷键,也可以选自己喜欢的其他键。
{ "keys": ["ctrl+m"], "caption":"SublimeREPL:Python", "command": "run_existing_window_command", "args":
{
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"}
},
保存之后,重新打开python文件,Ctrl+m弹出REPL窗口可以接收input数据了。
类似问题:运行C程序代码,无法scanf输入数据
解决办法:
Tools ->Build Systems -> 新建编译系统
新建编译系统,输入以下内容后,保存为C_cmd.sublime-build,运行时选中C_cmd进行编译执行。
{
"cmd": ["g++", "${file}", "-fexec-charset=gbk", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/c", "g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "start", "cmd", "/c", "${file_path}/${file_base_name} & pause"]
}
]
}
问题:import numpy报错
ImportError: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
Please note and check the following:
* The Python version is: Python3.8 from "C:\Users\zyy\Anaconda3\envs\env_pytorch\python.exe"
* The NumPy version is: "1.20.3"
解决办法:
确认numpy版本,不符合要求卸载重装
pip uninstall numpy
pip install numpy
问题:matplotlib画图不显示
解决办法:
在python_conda编译系统下(添加“shell”:true
后)可以正常运行
Tools ->Build Systems -> 新建编译系统,或打开现有的编译系统进行更改
如打开编译系统文件进行更改:
C:\Users\zyy\AppData\Roaming\Sublime Text 3\Packages\User\python_conda.sublime-build
{
"cmd": ["C:\\Users\\zyy\\Anaconda3\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "cp936", //防止出现中文乱码
"shell":true //画图时弹出窗口
}
问题:希望sublime text 出现jupyter notebook的效果
解决办法:
安装插件Helium(原来叫Hermes)
Ctrl+Shift+P,输入install,打开Package Control:Install Package
,输入Helium安装,或者在github.com/pykong/Helium下载安装包。以上地址会自动跳转到
https://github.com/sschuhmann/Helium,打包下载后,解压到
C:\Users\zyy\AppData\Roaming\Sublime Text 3\Packages文件夹下面
网上关于Helium的文档资料比较少,自带的Readme也不太详细。
参考了几个文档,如
https://stackoverflow.com/questions/61766361/unable-to-run-helium-with-sublime-text
自己摸索了一下,勉强跑起来了,具体步骤如下:
步骤 1) 在命令行窗口先启动 jupyter notebook,新建或打开一个已有的.ipynb,在cell中运行 %connect_info
,并将运行结果复制到剪贴板
{
"shell_port": 16760,
"iopub_port": 16761,
"stdin_port": 16762,
"control_port": 16763,
"hb_port": 16764,
"ip": "127.0.0.1",
"key": "d4dcef1d-eab0760309c45476556ebcb5",
"transport": "tcp",
"signature_scheme": "hmac-sha256",
"kernel_name": ""
}
Paste the above JSON into a file, and connect with:
$> jupyter <app> --existing <file>
or, if you are local, you can connect with just:
$> jupyter <app> --existing kernel-8eac6669-6673-4931-ac21-8202928357ba.json
or even just:
$> jupyter <app> --existing
if this is the most recent Jupyter kernel you have started.
实际上,这个json文件存放在C:\Users\zyy\AppData\Roaming\jupyter\runtime下。
步骤 2) 在 sublime 文本编辑器中,按“Ctrl+shift+P”并输入 Helium: start kernel
,选择 Enter connection info
回车,在下方弹出窗口提示Enter connection info or the path to connection file
,在右侧空白处将从步骤 1 中复制的内容粘贴在这里。回车输入connection name
,如python,回车。sublime弹出新窗口*Helium Output*python(balabala...)
步骤 3) 打开要执行的 python 文件,然后使用鼠标选择要执行的行,然后再次按“Ctrl+shift+P”并键入 Helium(现在你将看到一系列Helium:开头的命令),选择Helium:execute cell
回车,选中行的输出结果应该在第 2 步中弹出的窗口上显示。重复这个步骤,可以看到代码块运行结果。其他操作与在jupyter notebook中类似,如果希望显示图形,python程序代码中同样要有%matplotlib inline
这条命令。
注意:如果您的 localhost jupyter notebook 在特定文件夹中运行,请确保要使用 helium 运行的 py 文件位于该 jupyter notebook 访问权限下,基本上 py 文件必须位于你从中运行 jupyter 的同一文件夹下.
到此为止,sublime text 4环境就配置的差不多了,可以开始编程实验了。