集成终端中的VS代码切换Python版本

2024-10-02 20:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Windows 10计算机上安装了VS Code v1.44.2和Microsoft的^{}扩展

根据{}文档,它能够在新启动的集成终端窗口中设置正确的python版本

这种功能在某种程度上是有效的:对我来说,只有从版本选择器中选择的Python版本是3.5.2——微软的ML服务器附带的版本时,这种功能才有效。而且,只有当正在启动的集成终端是cmd时,它才起作用

当满足这两个狭窄条件时,可以肯定的是,只要启动集成终端,vscode-python就会运行/path/to/ML Server/Python/executable/activate.bat,如果在此后的命令提示符下运行python --version,您就会看到:

Python 3.5.2 :: Anaconda 4.2.0 (64-bit)

不幸的是,环境激活在我安装的其他几个Python版本中不起作用:2.7.x3.6.x3.7.x:不在cmdgitbash上,我怀疑在许多其他shell上也不起作用

查看vscode-python源代码,原因很明显:vscode-python在与python.pythonPath相同的文件夹中查找activate脚本(activate.batactivate.shactivate),而这些脚本通常位于python.pythonPath/Lib/venv/scripts的子文件夹中

来自vscode-pythonrepo(src/client/common/terminal/environmentActivationProviders/baseActivationProvider.ts)的相关代码片段复制如下:

    protected async findScriptFile(pythonPath: string, scriptFileNames: string[]): Promise<string | undefined> {
        const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
        for (const scriptFileName of scriptFileNames) {
            // Generate scripts are found in the same directory as the interpreter.
            const scriptFile = path.join(path.dirname(pythonPath), scriptFileName);
            const found = await fs.fileExists(scriptFile);
            if (found) {
                return scriptFile;
            }
        }
    }

此方法从src/client/common/terminal/environmentActivationProviders/bash.ts调用:

   public async getActivationCommandsForInterpreter(
        pythonPath: string,
        targetShell: TerminalShellType
    ): Promise<string[] | undefined> {
        const scriptFile = await this.findScriptFile(pythonPath, this.getScriptsInOrderOfPreference(targetShell));
        if (!scriptFile) {
            return;
        }
        return [`source ${scriptFile.fileToCommandArgument()}`];
    }

    private getScriptsInOrderOfPreference(targetShell: TerminalShellType): string[] {
        switch (targetShell) {
            case TerminalShellType.wsl:
            case TerminalShellType.ksh:
            case TerminalShellType.zsh:
            case TerminalShellType.gitbash:
            case TerminalShellType.bash: {
                return ['activate.sh', 'activate'];
            }
            case TerminalShellType.tcshell:
            case TerminalShellType.cshell: {
                return ['activate.csh'];
            }
            case TerminalShellType.fish: {
                return ['activate.fish'];
            }
            default: {
                return [];
            }
        }
    }

从这些代码片段可以明显看出,如果集成终端是gitbashvscode-pythonpython.pythonPath中查找activate.shactivate,但不在其子目录中

我在网上寻找工作机会的结果并不多。有些答案依赖于.bashrc.bash_profile中的别名或源代码激活,但这并不能解决我的问题

理想情况下,我希望具有以下功能:

  1. 当我通过VS代码(命令面板或状态栏)设置Python版本时,如果有一个集成终端打开,它应该自动切换版本。我想同样的想法也适用于环境,而不是版本
  2. 当我启动集成终端时,它应该以正确的版本集启动。如果需要,我可以自动调用相应的activate文件

我确信其他vscode-python用户也遇到了同样的问题,从代码片段来看,解决方案似乎很简单(允许搜索/venv/**/*);只是想看看是否有不同的方式来实现我的最终目标


Tags: path代码功能版本终端stringreturnvscode
1条回答
网友
1楼 · 发布于 2024-10-02 20:32:08

没有人回答这个问题? 这里有一个链接,指向您应该使用的设置。 我在windows上运行MiniConda3,但你可以为Mac调整它。 我为数据科学的不同课程构建了几个环境。当我打开VS代码时,终端被设置为我想要的Python版本。命令提示符。此外,Jupyter或terminal窗口也设置了正确的版本

https://medium.com/analytics-vidhya/efficient-way-to-activate-conda-in-vscode-ef21c4c231f2

相关问题 更多 >