【小工具】如何在 VsCode 下“自由”地运行 Python 脚本和模块

  • Post author:
  • Post category:python




1. 需求

我知道。VsCode 是自带运行 Python 的方法的,也就是按下 F5,即可进入调试运行。如果不喜欢这一款式,也可以自己下载一个 Code Runner 插件一键运行。

但是我有特别的需求:

  1. 可以自由切换 Conda 环境运行
  2. 可以作为独立的外部进程运行
  3. 可以不依赖于工程 launch.json 的配置

一条一条地说。



自由切换Conda环境运行

比如在某一个工程目录下,有两个文件:

main_tf.py



main_torch.py

。这两个脚本对应的环境是不太一样的。我希望可以自由切换环境来运行不同脚本。

当然 VsCode 下可以通过切换“这个”来运行不同的 Conda 环境。但我嫌每次都要选一下很烦,很 ugly。

在这里插入图片描述



可以作为独立的外部进程运行

VsCode 想在外部运行唯一的方法是在 launch.json 中设置

"console": "externalTerminal"

。不过这也不太行,一旦 VsCode 关掉,程序运行就结束了。我想要的是独立于 VsCode 的运行。



可以不依赖于工程 launch.json 的配置

也就是可以独立于工程单独运行。



2. 解决

为了解决这个问题,我写了两个脚本:

  • CondaRunScript.ps1
param([switch]$external);
# $args: (envname, script_path, module_args)

[string]$envname = $args[0]
[string]$script = $args[1]
[string]$otherargs = $args[2..$args.Count]

$RunScript = {
    param($envname, $script, $otherargs);
    activate;
    conda activate $envname;
    python $script $otherargs;
}

if ($external) {
    # 在其他进程内运行
    start-process powershell -ArgumentList "-NoExit -Command 
        & {$RunScript; pause} $envname $script $otherargs";
}
else {
    # 运行
    &$RunScript $envname $script $otherargs;
}

  • CondaRunModule.ps1
param([switch]$external);
# $args: (envname, module_path, module_args)
[string]$envname = $args[0]
[string]$module = $args[1]
[string]$otherargs = $args[2..$args.Count]

# 更改 module
$sep = [IO.Path]::DirectorySeparatorChar;
$module = $module.Replace('.py', '').Replace($sep, '.');

$RunModule = {
    param($envname, $module, $otherargs);
    activate;
    conda activate $envname;
    python -m $module $otherargs;
}

if ($external) {
    # 在其他进程内运行
    start-process powershell -ArgumentList "-NoExit -Command 
        & {$RunModule; pause} $envname $module $otherargs";
}
else {
    # 运行
    &$RunModule $envname $module $otherargs;
}

然后把他们放到一个在环境变量 Path 下的路径。比如我就直接放在了 Anaconda 路径下。

然后配置 User Task(或者本地的 User Task 也行)。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run Conda Script",
            "type": "shell",
            "command": "CondaRunScript.ps1",
            "args": [
                "torch",
                "${relativeFile}",
            ],
            "problemMatcher": []
        },
        {
            "label": "Run Conda Script External",
            "type": "shell",
            "command": "CondaRunScript.ps1",
            "args": [
                "torch",
                "${relativeFile}",
                "-external"
            ],
            "problemMatcher": []
        },
        {
            "label": "Run Conda Module",
            "type": "shell",
            "command": "CondaRunModule.ps1",
            "args": [
                "torch",
                "${relativeFile}",
            ],
            "problemMatcher": []
        },
        {
            "label": "Run Conda Module External",
            "type": "shell",
            "command": "CondaRunModule.ps1",
            "args": [
                "torch",
                "${relativeFile}",
                "-external"
            ],
            "problemMatcher": []
        }
    ]
}

然后配置一下 Run Task 的快捷键

在这里插入图片描述

搞定。



版权声明:本文为frostime原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。