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

博客介绍了如何解决在VSCode中运行Python脚本时,需要自由切换Conda环境、作为独立进程运行且不依赖launch.json配置的问题。作者通过编写两个PowerShell脚本(CondaRunScript.ps1和CondaRunModule.ps1)并配置UserTask,实现了在VSCode外部独立运行脚本,并能选择特定的Conda环境。此外,还提供了快捷键配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 需求

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

但是我有特别的需求:

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

一条一条地说。

自由切换Conda环境运行

比如在某一个工程目录下,有两个文件:main_tf.pymain_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 的快捷键
在这里插入图片描述
搞定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值