新建编译环境
在sublime菜单栏中Tools => Build System => New Build System...,输入一下内容并保存为 Python3.sublime-build 。
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {
"LANG": "en_US.UTF-8" // 如果不指定编码,会出现print('中文')乱码
}
// 编码指定utf-8方法2:
// "env" : {
// "PYTHONIOENCODING": "utf8"
// }
}
其中 /usr/local/bin/python3 为python的具体路径,可通过 which python3 获得。
Anaconda插件
插件简介
实用的python插件,用多项类似IDE的功能:
Autocompletion 代码自动完成
Code Linting 代码语法、格式检查
Goto Definitions 查找显示变量、函数、类的定义
Find Usages 查找变量、函数、类的实用
……
安装
通过 Package Control 搜索 anaconda 安装
配置
{
// Python主文件位置
"python_interpreter": "/usr/local/bin/python3",
// 语法格式检查 <= (仅在保存时检查,避免coding过程中一直出现警告框)
"anaconda_linting": true,
"anaconda_linting_behaviour": "save-only", // 保存时检查
"anaconda_gutter_theme": "hard",
"anaconda_linter_show_errors_on_save": false, // 保存时显示错误
"anaconda_linter_phantoms": true, // 界面显示错误
// pep8自动格式化
"auto_formatting": true,
"pep8_ignore": [
"E501",
],
// 文档显示设置
"enable_docstrings_tooltip": true, // 显示文档
"enable_signatures_tooltip": true, //在悬浮窗中显示方法签名
"display_signatures": true, //显示方法签名
"merge_signatures_and_doc": true
}
解决模块名无法正常补全
问题
ST3当检测到一些单词(如: class、def、import等)时取消了python包的自动补全。
解决方法
在 Preferences/Browser Packages 打开 Packages 目录,新建 Python 目录,新建 Completion Rules.tmPreferences 文件并输入以下内容,然后重启ST3。
scope
source.python
settings
cancelCompletion
^(.*\b(and|or)$)|(\s*(pass|return|and|or|(class|def)\s*[a-zA-Z_0-9]+)$)
Unicode编码问题
问题
UnicodeEncodeError: 'ascii' codec can't encode characters in position 294-302: ordinal not in range(128)
原因
sublime控制台ASCII 编码无法对 unicode 的中文进行编码,编译环境容需要指定编码。
解决方法
在 Python3.sublime-build 中指定编码:
{
"env": {
"LANG": "en_US.UTF-8" # 如果不指定编码,会出现print('中文')乱码
}
}
# 或者
{
"env" : {
"PYTHONIOENCODING": "utf8"
}
}