Sublime Text4 配置、运行python

本文最后更新于:2024年3月20日 中午

前言

win10 系统下关于sublime text 4 怎么运行python

系统:win10

编辑器:sublime TXT 4

-——————————–

有两种方法可以在sublime中直接运行python代码。

1. 非交互式程序写完代码保存后直接按ctrl+B,如只有输出的程序。

2. 交互式程序需要下载SublimeREPL,如需要输入的的程序。

1. 非交互式程序

1. 设置:

工具(tool)–>编译系统(build-system)–>设置为自动,或者选择配置python

打开sublime text4–tools–build system–new build system
输入(*******为python安装路径,例:"D:/anaconda3/python.exe"

1
2
3
4
5
{
"cmd": ["********","-u","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
}

保存在sublime text4 --package--users
注意一下找安装路径(在写时要把 \ 改成 /

2. 编辑代码

1
print("hello python.")

保存为test.py

3. 执行

ctrl+B,可在sublime界面下方弹出结果。

注:确保已经正确配置好python的环境变量(PATH)

2. 交互式环境

准备代码,保存为test.py

1
2
3
msg = input("please input: ")

print("output: ", msg)

1. 按ctrl+shift+p快捷键呼出一个输入框,输入Install Package,回车,在新出现的输入框里输入SublimeREPL安装。

2. 点击Tools->sublimeREPL->python->python run current file,这时候就像IDLE一样,会弹出一个新的窗口,而且是可交互的,可以输入。

3. 结果:

4. 设置快捷键

点击preferences->key bindings

添加:

1
2
3
4
5
6
7
8
9
{ "keys":["f5"],

"caption": "SublimeREPL: Python - RUN current file",

"command": "run_existing_window_command",

"args": {"id": "repl_python_run","file": "config/Python/Main.sublime-menu"}

}

重启sublime,现在可以直接按F5来运行。