参见https://www.cnblogs.com/net66/p/5598383.html
我ubuntu下有python的多个版本。有次,press ctrl+b,提示我选择版本,选了某贝尔版本,发现提示我要装yaml等一些库。先前的python是anaconda下的python,yaml等库都已经有了。
当然可以在编译系统里增加一个专门针对.py进行编译的命令,但是我希望能够自动判断文件类型,自动进行编译。在google搜寻了好一阵子,找到了一个临时性的方案。摘录如下。
安装PackageResourceViewer插件
- 输入
Ctrl+Shift+P
- 输入
install
,选择Package Control: Install Package
- 选择
PackageResourceViewer
,安装
设置默认的 Python.sublime-build
- 输入
Ctrl+Shift+P
- 输入
resource
,选择PackageResourceViewer:Open Resource
- 再选择
Python
,再再选择Python.sublime-build
- 编辑
Python.sublime-build
将"shell_cmd": "python -u "$file"",
改为以下之一:
"shell_cmd": "python3 -u "$file"", //指定python3为.py默认编译器
"shell_cmd": "python2 -u "$file"", //指定python2为.py默认编译器
"shell_cmd": "python -u "$file"", //根据Ubuntu系统设置,看/usr/bin/python链接哪儿(ln)
"shell_cmd": "指定版本python的绝对路径 -u "$file"", //指定路径下的python编译器
- 使用python3的配置文件示例(Python.sublime-build)
{
//"shell_cmd": "python -u "$file"",
"shell_cmd": "python3 -u "$file"", //指定python3为.py默认编译器
"file_regex": "^[ ]*File "(...*?)", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "python -m py_compile "${file}"",
}
]
}
- Ctrl+S 保存配置文件
注:有关.sublime-build
的配置信息说明,可见参见这儿- 重启Sublime Text 3
- 打开.py文件,Ctrl + B 即可编译执行
呵呵,方便、顺眼多了
与其他方法的使用比较
网上也有其他变通方法,可以参考下面链接:
个人感觉:
我更改那个文件是这么更改的。
"shell_cmd": "/opt/anaconda3/bin/python -u "$file"", ...
把绝对路径给加上了。
想来我第一次按ctrl+B时,我选择了某个python版本,应该是sublime记住了这个python版本。以后再press ctrl+B时,系统就不认可环境变量里python到底是指向哪个的,而是把以前保留下来的python指向的版本给调出来。
类似的技巧也可以用在gcc,g++上。这个.sbulime-build文件关闭后,你再想打开它,是打开不了的。我估计是放在某个压缩包里了。一旦关闭文件,那个临时解压后的文件就不存在了,只有依赖PackageResourceReview打开才有效。
"shell_cmd": "/opt/anaconda3/bin/python -u "$file"",
故障解决
有次我把“export LD_LIBRARY_PATH=/opt/anaconda3/lib/:$LD_LIBRARY_PATH”放到 ~/.profile中,结果发现系统无法登录进去。改放到~/.bashrc里就好了。另外,如果没有在~/.bashrc中配好的话,也可以修改python解释器的内容为:
{ "path": "/opt/anaconda3/bin", // "cmd": [ // "python", // "-u", // "$file" // ], "shell_cmd": "export LD_LIBRARY_PATH=/opt/anaconda3/lib/:$LD_LIBRARY_PATH && python -u "$file"", "file_regex": "^[ ]*File "(...*?)", line ([0-9]*)", "selector": "source.python" }
注掉的内容是以前的。现在加了一条“export……”,这下子在sublime中运行脚本就没有问题了。