首页 > 代码库 > sublime text中用python3运行文件

sublime text中用python3运行文件

Edit

sublime text中用python3运行文件

在sublime text中可以使用快捷键cmd + b(windows是CTRL + b)快速运行一个python文件,sublime会直接调用系统默认python运行文件,并在下面的框里弹出结果。

sublime中直接运行python效果图

如果我们想要使用自己的python版本,如python3或者其他第三方python包(如本人使用的anaconda)要怎么办呢?

sublime使用cmd+b的操作叫做build,在Tools -> Build System中可以看到系统默认支持的类型,包括C++Java等各种,默认选择的是Automatic,系统会根据文件扩展名自动选择合适的Build方法。

Alt text

操作方法

  1. 要使用自定义达到方法,选择下面的New Build System...,会弹出一个后缀为sublime-build的文件。
    Alt text
  2. 在打开的文件中填写如下内容:
    {
     "cmd": ["/usr/local/bin/python3", "-u", "$file"],
     "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
     "selector": "source.python"
    }
    
    然后保存文件名为python3.sublime-build,文件会自动保存到sublime的自定义文件夹中,mac下默认是~/Library/Application Support/Sublime Text 2/Packages/User
    Alt text
  3. 重新进入刚才的Build System选项卡中,会发现下面增加了一个python3,选择该项。
    Alt text
  4. 再按cmd+b运行文件时,sublime就会用python3来运行该文件了。因为这个print语句不符合python3语法所以报错。
    Alt text

sublime text中用python3运行文件