Ubuntu 16.04 修改Sublime Text 3 默认python编译版本

本文介绍如何在Sublime Text 3中安装PackageResourceViewer插件,并通过修改Python.sublime-build文件来指定Python解释器版本,实现对.py文件进行编译执行。

参考资料:https://www.cnblogs.com/net66/p/5598383.html

安装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的配置文件示例1(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}\"",
        }
    ]
}
  • 使用python3的配置文件示例2(Python.sublime-build)

{
    //"shell_cmd": "python -u \"$file\"",
    "shell_cmd": "/home/yy430/anaconda3/bin/python -u \"$file\"",        //指定anacondad的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 即可编译执行


<think>我们之前已经回答过类似问题,但需要根据当前查询重新组织。用户再次询问Ubuntu 16.04设置默认PythonPython3,我们需要注意安全性和步骤清晰。 由于Ubuntu 16.04默认同时安装了Python 2.7和Python 3.5,但系统工具(如apt)依赖于Python 2.7,因此不推荐直接替换系统默认python链接。 推荐的方法有两种:用户级别的别名设置(安全)和系统级别的update-alternatives(需谨慎)。 此外,我们还可以建议使用虚拟环境。 步骤: 1. 安全方法(用户级别): - 在用户的shell配置文件(如.bashrc)中添加别名:alias python=python3 - 然后source ~/.bashrc使别名生效。 2. 系统级别方法(使用update-alternatives): - 首先安装Python 3.5到update-alternatives(如果尚未安装) - 然后使用update-alternatives命令设置默认版本。 3. 注意:不要删除/usr/bin/python2.7,也不要直接删除/usr/bin/python符号链接,否则可能导致系统问题。 同时,我们可以提醒用户,如果系统工具(如apt)出现错误,如何恢复。 最后,按照要求提出3-5个相关问题。 注意:回答末尾必须包含“§§相关问题§§”且第一行就是这个,然后列出问题。</think>在 Ubuntu 16.04 中将默认 Python 切换为 Python 3 的操作步骤如下(系统组件依赖 Python 2,请谨慎操作): ### 安全操作流程 1. **检查现有版本** ```bash ls /usr/bin/python* # 查看可用版本 python3 --version # 确认 Python 3 存在(默认 3.5) ``` 2. **设置用户级别名(推荐)** ```bash echo "alias python=python3" >> ~/.bashrc source ~/.bashrc ``` 3. **系统级修改(需管理员权限)** ```bash sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 10 sudo update-alternatives --config python # 选择 python3.5 对应编号 ``` 4. **验证更改** ```bash python --version # 应显示 Python 3.5.x ``` ### 关键注意事项 - ⚠️ **系统依赖警告**: `apt`、`gnome-terminal` 等核心工具依赖 Python 2.7,修改后若出现错误(如 `apt` 报错),立即还原: ```bash sudo update-alternatives --config python # 重新选择 python2.7 ``` - **虚拟环境方案**(更安全): ```bash sudo apt install python3-venv python3 -m venv myproject_env # 创建隔离环境 source myproject_env/bin/activate # 激活环境 ``` ### 故障恢复 若系统工具异常,强制还原默认配置: ```bash sudo ln -sf /usr/bin/python2.7 /usr/bin/python ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值