栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【小工具】如何在 VsCode 下“自由”地运行 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 的快捷键

搞定。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号