如何在 Visual Studio 代码中为 Python 设置虚拟环境?

How can I set up a virtual environment for Python in Visual Studio Code?

在我的项目文件夹中,我创建了一个 venv 文件夹:

python -m venv venv

当我在 Visual Studio 代码中 运行 命令 select python interpreter 时,我的 venv 文件夹没有显示。我按照建议 here 升了一级,但是 Visual Studio 代码看不到我的虚拟解释器。

我错过了什么?

激活您的环境。

你也可以试试这个:

Using Python environments in Visual Studio Code

P.S.:

  • 我使用 Visual Studio 代码有一段时间了,发现了另一种在 Visual Studio 代码中显示虚拟环境的方法。

  • 通过命令提示符转到 venv 所在的父文件夹。

  • 键入 code . 输入 。 [它正在为我工​​作 Windows 和 Linux。]

  • 这还应该显示该文件夹中存在的虚拟环境。

原答案

每次我使用 venv 编写 Visual Studio 代码时,我几乎 运行 都会遇到同样的问题。我按照以下步骤操作:

  1. 转到菜单 文件首选项设置.

  2. 单击工作区设置

  3. Files:Association 下,在 JSON: Schemas 部分,您会发现 在settings.json中编辑。点击那个。

  4. 在工作区设置下更新 "python.pythonPath": "Your_venv_path/bin/python"。 (对于 Windows):在工作区设置下更新 "python.pythonPath": "Your_venv_path/Scripts/python.exe"

  5. 重新启动 Visual Studio 代码以防它仍然不显示您的 venv.

注意: 对于较新的版本,请使用 python.defaultInterpreterPath 而不是 python.pythonPath

我遇到了同样的问题,这是因为 PowerShell 没有更新。有时 Windows 保留版本 2.* 而我不得不手动下载并安装版本 3.

之后问题就解决了,可以很好的使用虚拟环境了

我遇到了同样的问题,直到我发现我试图让我的项目目录和虚拟环境保持一致——这是不正确的。

我有一个 \Code\Python 目录,用于存储我所有的 Python 项目。 我的 Python 3 安装在我的路径上。

如果我想创建一个新的 Python 项目 (Project1),它有自己的虚拟环境,那么我会这样做:

python -m venv Code\Python\Project1\venv

然后,只需在 Visual Studio 代码中打开文件夹 (Project1) 即可确保使用正确的虚拟环境。

这是对 Sumit S Chawla 答案的补充,虽然它是正确的,但忽略了这样一个事实,即任何时候你在 Visual Studio 代码中打开一个文件夹,它都会创建一个 。vscode 文件夹,但可以是多个,在您最终打开目录时创建。

.vscode 文件夹有 JSON 个对象,其内容属性如“setting.json”,其中声明 interpreter 在 ".vscode" 级别使用(有关更多说明,请参阅 )。

{
   {
     "python.pythonPath": "VirtualEnPath/bin/python3.6"
   }
}

因此您可能会在虚拟环境中的另一个级别打开 Visual Studio 代码。它创建了另一个 .vscode 文件夹,假定为 Python 目录,这些文件夹是全局机器的目录,因此出现这样的错误,如果虚拟环境是否激活

这确实发生在我身上。我确实有一个 DjangoRESTAPI_GEN 文件夹,我最初在其中打开了 IDE,它确实识别了虚拟环境 Python 路径。然后几天后,我在 Git 所在的级别打开它,所以它确实创建了另一个 .vscode 文件夹,该文件夹选择了全局 Python 解释器,导致我在虚拟环境中的lint没有被使用。

并且虚拟环境解释器甚至没有显示在“select python 解释器”中。但正如所写,在 .vscode 文件夹所在的级别打开 IDE,其中包含 settings.json 文件的正确路径,确实如此。

一旦你在 setting.json 文件和 select 虚拟环境解释器中设置了正确的路径,那么 Visual Studio 代码将自动激活终端中的虚拟环境:

许多人提到了 python.pythonPath 方法。

另一种方法是在 launch.json 中添加一个 envFile,如下所示:

    {
        "name": "Run",
        "etc": "etc",
        "envFile": "${workspaceFolder}/venv"
    }

我在不更改 Python 路径的情况下解决了这个问题,因为这对我来说似乎不是正确的解决方案。以下解决方案对我有用,希望对你也有用:))

  1. 在Windows中打开cmd / Linux/Mac中的shell。

  2. 激活你的 virtualenv(使用 source activate / activate.bat / activate.ps1 如果使用 PowerShell)

    C:\Users\<myUserName>\Videos\myFolder>django-project\Scripts\activate.bat (django-project) C:\Users\<myUserName>\Videos\myFolder>

  3. 导航到您的项目目录并打开 Visual Studio 代码。

    (django-project) C:\Users\prash\Videos\myFolder\projects>code .

  4. 在Visual Studio代码中,转到菜单文件首选项设置(不用担心你不需要打开JSON文件)

  5. 在设置搜索栏中,搜索 virtual / venv 并按 Enter。您应该在搜索栏中找到以下内容:

Python: Venv Folders Folders in your home directory to look into for virtual environments (supports pyenv, direnv and virtualenvwrapper by default).

  1. 添加一个项目,然后输入你的virtuanenv的脚本路径,里面有激活文件。比如在我的系统中是:

    C:\Users\<myUserName>\Videos\myFolder\django-project\Scripts\

  2. 保存并重启Visual Studio代码。

  3. 要重新启动,请再次打开 cmd,导航到您的项目路径并打开 Visual Studio 代码。 (请注意,在打开 Visual Studio 来自 cmd 的代码之前,应在 cmd 中激活您的 venv)

Command to open Visual Studio Code from cmd:

code .

我遇到了同样的问题,解决方法很简单:

"If you create a new conda environment while VS Code is running, use the Reload Window command to refresh the environment list shown with Python: Select Interpreter; otherwise you may not see the environment there. It might take a short time to appear; if you don't see it at first, wait 15 seconds then try using the command again."

那是写在 Visual Studio Code site 上的。

注意:要重新加载 window:Ctrl + Shift + P in Visual Studio代码,然后写reload window

有一个名为“Python Auto Venv”的 Visual Studio 代码扩展,如果有的话,它会自动检测并使用您的虚拟环境。

使用较新的 Visual Studio 代码版本非常简单。

打开项目文件夹中的 Visual Studio 代码。

然后打开Python终端 (Ctrl + Shift + P: Python: 创建终端)

在终端中:

python -m venv venv

然后您将看到以下对话框:

点击;你的 venv 已经准备好了。

在VSCode中打开一个新终端 Ctrl + Shift + P你会看到 venv 被拾起;例如:(venv) ...

您现在可以像往常一样安装软件包,例如 pip install sklearn

跟踪已安装的内容:pip freeze > requirements.txt


对于 VSCode 的旧版本,您可能还需要执行以下操作:

然后Python: Select Interpreter(通过Ctrl + Shift + P

和select选项(在我的情况下朝向底部)

Python 3.7 (venv) ./venv/Scripts/python.exe

如果你看到

Activate.ps1 is not digitally signed. You cannot run this script on the current system.

您需要执行以下操作:

有关详细信息,请参阅:Global, virtual, and conda environments

正在安装模块

Ctrl + Shift + P and 终端:新建集成终端

从终端

Windows: .\.venv\Scripts\activate

Linux: .\.venv\bin\activate

您现在可以照常安装软件包,例如 pip install sklearn

对于Jupyter, you need to do more - Jupyter notebooks in Visual Studio Code does not use the active virtual environment

在Visual Studio代码中,select一个文件夹,创建一个工作区就可以了。

对于 Anaconda 用户:只需使用 Conda, see 创建一个 venv。然后,打开 Visual Studio 代码并左键单击左下角 Visual Studio 代码中显示的 Visual Studio 代码解释器:

选择一个在设置下拉列表中弹出的虚拟环境 window,您就完成了。

对于Mac用户,请注意这个错误:当您点击“输入解释器路径”时,您有两种选择:(1) 手动输入路径; (2) select 来自 Finder 的 venv 文件。

只有我手动输入路径才有效。使用 Finder 选择会产生一些奇怪的路径,例如我理解的 Library/Developer/CommandTools/...

  1. 如果您在 Mac 上使用 Visual Studio 代码,请务必将您的 venv 安装在与您的工作区相同的目录中。

  2. 在我的例子中,我的 venv 在不同的目录中(不在我的项目工作区中),所以我的 venv 到项目工作区的简单 cut/copy-paste 就可以了。

  3. 一旦您的 venv 被复制到项目工作区,您的 Visual Studio 代码就会选择它并显示一条通知,让您可以选择 select venv 作为口译员。

创建虚拟环境的步骤:

  1. 转到包含项目的文件夹
  2. python3 -m venv evn_name
  3. source evn_name/bin/activate
  4. 现在您将能够在每个终端行前面看到 (env_name)

现在您可以在虚拟环境中安装所需的库

  1. pip3 install -r requirement.txt
  2. 如果需要重启代码编辑器

停止在虚拟环境中工作类型:deactivate

删除虚拟环境类型:rm -rf evn_name

假设您已经创建了一个名为 venv 的 virtualenv 文件夹。

您可以通过在安装 venv 的目录中键入以下命令轻松激活它。

.\venv\Scripts\activate

如果您已经有了 virtualenvs,您只需打开 VSCode 首选项 (Ctrl + ,) 并搜索 venv。然后将 virtualenvs 的路径添加到 “Venv Path” 设置中,如下所示:

可在此处找到更多信息: https://techinscribed.com/python-virtual-environment-in-vscode/

我确定的两个主要问题可能导致无法看到 venv。

  1. 如果您使用的是 VS Code 版本 1.60.0 或更接近该版本,则 python.defaultInterpreterPath 给出了 venv 的路径。 python.pythonPath 不再有效。

所以,settings.json 应该是这样的:

{
        "python.defaultInterpreterPath": 
    "C:\tproj\tproj_env\Scripts\python"
    }
  1. venv 文件夹应位于比 settings.json 所在位置更高级别的文件夹中。 例如:如果项目名称是 tproj 并且 venv 文件夹(例如 tproj_env)和代码文件夹(例如 tproj_code)是子文件夹。建议将 settings.json 放在 tproj_code 子文件夹中。

VS Code python 扩展自动检查默认虚拟环境目录中的虚拟环境:https://code.visualstudio.com/docs/python/environments#_where-the-extension-looks-for-environments

如果您的虚拟环境存在于任何这些默认目录中,您只需要通过 select 激活它即可。要 select 特定环境,请使用命令面板 (Ctrl+Shift+P) 中的 'Python: Select Interpreter' 命令。 参考:https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment

以管理员权限启动 PowerShell,运行 以下命令:

 Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Remotesigned

确认并开始!执行策略更改已更新。您可以返回 VsCode 并激活您的虚拟环境。

你必须select python 就是在虚拟环境中

  1. 创建新的 venv (virtualenv -p python3 venv)
  2. 在 Vs Code 中打开目录(使用 venv)
  3. CMD + Shift + P: Python: Select 来自 venv 的解释器

github