我有VS Code v1.44.2和微软的vscode-python扩展安装在Windows 10机器上。
根据vscode-python文档,它能够在新启动的集成终端窗口中设置正确的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.x、3.6.x、3.7.x:不在cmd或gitbash上,我怀疑许多其他的shell上也没有。
查看vscode-python源代码,原因很明显:vscode-python在与python.pythonPath相同的文件夹中查找activate脚本(activate.bat、activate.sh、activate),而这些脚本通常位于python.pythonPath/Lib/venv/scripts的子目录中。
vscode-python repo (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 [];
}
}
}从这些代码片段中可以看出,如果集成终端是gitbash,则vscode-python在python.pythonPath中查找activate.sh或activate,而不是在其子目录中查找。
我在网上搜索周围的作品并没有产生什么效果。有些答案依赖于.bashrc或.bash_profile中的别名或资源激活,但这并不能解决我的问题。
理想情况下,我希望具有以下功能:
当我通过VS代码(命令调色板或状态栏)设置版本时,如果有一个完整的打开终端,它应该自动切换版本。我认为同样的想法也适用于环境,与versions.
activate文件。我确信其他vscode-python用户也遇到了同样的问题,从代码片段来看,解决方案看起来很简单(允许搜索到/venv/**/*);只是想看看是否有一种不同的方式来实现我的最终目标?
发布于 2021-01-07 22:40:57
没人接这个?下面是您应该使用的设置的链接。我正在windows上运行MiniConda3,但您可以对此进行调整。我为不同的类构建了几个用于数据科学的环境。当我打开VS代码时,终端被设置为我想要的Python版本。CMD提示。此外,正确的版本被设置为木星或终端窗口。
https://medium.com/analytics-vidhya/efficient-way-to-activate-conda-in-vscode-ef21c4c231f2
https://stackoverflow.com/questions/61515698
复制相似问题