在 Visual Studio Code 中自动激活虚拟环境 [英] Auto activate virtual environment in Visual Studio Code

查看:75
本文介绍了在 Visual Studio Code 中自动激活虚拟环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 VS Code 在运行时打开 venv,但我找不到如何做到这一点.我已经尝试将这一行添加到 settings.json:

I want VS Code to turn venv on run, but I can't find how to do that. I already tried to add to settings.json this line:

"terminal.integrated.shellArgs.windows": ["source${workspaceFolder}\env\Scripts\activate"]

"terminal.integrated.shellArgs.windows": ["source${workspaceFolder}\env\Scripts\activate"]

但是,它向我抛出了 127 错误代码.我发现了 127 代码的含义.这意味着,未找到.但是怎么找不到呢,如果我现在看到我的 venv 文件夹在我眼中?

But, it throws me an 127 error code. I found what 127 code means. It means, Not found. But how it can be not found, if I see my venv folder in my eyes right now?

我认为是终端故障.我正在使用带有 Git Bash 终端的 Win 10,这是在您将 Git 安装到您的机器时提供的.

I think it's terminal fault. I'm using Win 10 with Git Bash terminal, that comes when you install Git to your machine.

推荐答案

您根本不需要这一行.只需删除它并切换您的 Python 解释器以指向您的 venv 中的那个.这是相关文档(斜体强调我的):

You don't need this line at all. Just remove it and switch your Python interpreter to point to the one within your venv. Here's a relevant documentation (italicized emphasis mine):

要选择特定环境,请使用 Python:选择解释器命令面板中的命令 (Ctrl+Shift+P).

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

...和使用终端:创建新的集成终端打开终端命令.在后一种情况下,VS Code 会自动激活选择的环境.

... and opening a terminal with the Terminal: Create New Integrated Terminal command. In the latter case, VS Code automatically activated the selected environment.

切换解释器后,VS 代码应在工作区中创建一个 .vscode 文件夹,其中 settings.json 表示 python 解释器.这将为 VS 代码提供定位 venv 的方向.

Once you switch the interpreter VS code should create a .vscode folder within your workspace with a settings.json indicating the python interpreter. This will give VS code the direction of where to locate the venv.

这篇关于在 Visual Studio Code 中自动激活虚拟环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆