Visual Studio 代码未检测到虚拟环境

Visual Studio Code does not detect Virtual Environments

Visual Studio 代码不检测虚拟环境。我运行vscode在venv文件夹所在的文件夹,当我尝试selectvscode中的内核时,我可以看到主环境和一个位于其他地方的环境磁盘。 vscode 中的 Jupyter 运行ning 也看不到这个环境。我已经在这个环境中安装了ipykernel。我尝试重新安装 vscode 和 python 扩展程序。

我试过在settings.json里面设置路径.vscode:

{
    "python.pythonPath": ".\venv\Scripts\python.exe"
}

Windows 10
Python3.6.7(64 位)
VSCode1.54.3

好的,我找到了解决办法。 首先卸载 Visual Studio 代码。转到 C:\Users\Your_profile 并删除与 Visual Studio 以句点开头的代码相关的文件夹。然后打开显示隐藏文件夹并转到 C:\Users\Your_profile\AppData。在文件查找器中输入 vscode 并删除所有与 Visual Studio 代码相关的文件夹和文件。最后,安装 Visual Studio 代码并享受虚拟环境。 :)

The only solution I found was to delete the venv and recreate it. I followed these steps but I'll provide a brief summary for Windows:

  1. Activate your virtualenv. Go to the parent folder where your Virtual Environment is located and run venv\scripts\activate. Keep in mind that the first name "venv" can vary.
  2. Create a requirements.txt file. pip freeze requirements.txt
  3. deactivate to exit the venv
  4. rm venv to delete the venv
  5. py -m venv venv to create a new one
  6. pip install -r requirements.txt to install the requirements.

这对我有用,我没有删除旧的,而是在 ~/Envs 文件夹 C:\Users\Admin\Envs 中创建了一个新的 python -m venv /path/newVenv。也许 VS Code 正在 ~/Envs 文件夹中搜索,或者需要将其添加到视图 -> 命令面板 -> >首选项中的 python.path:打开用户设置。

  1. 在 VSCode 中打开您的命令面板 — 默认情况下 Ctrl+Shift+P

  2. 寻找Python: Select Interpreter

  3. Select Interpreter中选择Enter interpreter path...然后Find...

  4. 导航到您的 venv 文件夹 — 例如,~/pyenvs/myenv/\Users\Foo\Bar\PyEnvs\MyEnv\

  5. 在虚拟环境文件夹中选择<your-venv-name>/bin/python<your-venv-name>/bin/python3


问题是 VSCode 的 Python 扩展默认使用主 pythonpython3 程序,而 venv 有效地创建了一个“新" python/python3 可执行文件(这是 venv 的重点)因此扩展无法访问您通过安装的任何内容(可用模块、名称空间等) venv 因为 venv 特定安装对主 Python 解释器不可用(同样,这是设计使然——就像安装在 VM 中的应用程序对主机不可用一样 OS)

None 关于此线程的建议对我有用。也就是说,我认为问题不在于 VS Code,而是 venv。我最终安装了 PyCharm 来解决这个问题。下载后:

PyCharm > Preferences > search “interpreter” > Project: Python Interpreter > 单击“+” > 在 Virtualenv Environment > New environment(应自动填充新环境的所有内容)。 Select好的好的好的。

在左下方,您会看到 Git |待办事项 |问题 |终端...等单击“终端”,您应该会看到您的环境已经激活。从那里,pip3 安装你的依赖项。关闭 PyCharm.

返回 VS Code,打开你的项目,然后按照上面的建议 select Virtualenv(我的是 'venv': venv)作为你的解释器。

终于解决了。

如果您是 Linux 用户,并且您已使用此方法或类似方法创建虚拟环境:

python3 -m venv venv

并且您无法使调试工作,删除您的 venv 并从 VS Code 终端创建它(单击 Ctrl + back-tick 打开)。

当你从 VS Code 终端创建它时,VS Code 会询问你是否要使用它为这个工作区检测到的这个新环境,说是。

这里的部分混淆可能源于 UI 与 VScode 文档不一致的行为。 docs 状态:

When you create a new virtual environment, a prompt will be displayed to allow you to select it for the workspace.

我的情况没有发生(VScode 1.66.2 运行 on Windows 10 with Remote - WSL plugin version 0.66.2)。我遵循了概述的步骤 here;我没有看到 VScode 文档描述的 pop-up,但是单击状态栏中的 Python 解释器版本显示 VScode 已自动选择安装在虚拟机中的解释器环境。此外,我确实观察到 VScode 正在采购 .venv/bin/activate,如上面 post 链接中所述

Run the code by clicking the play button, note the .venv and source “/Users/jemurray/Google Drive/scripts/personalPython/helloworld/.venv/bin/activate” in the terminal shows the script is activated and running in the virtual environment