简体   繁体   English

如何从 Visual Studio 代码中的现有虚拟环境中设置 Python 解释器

[英]How to set a Python Interpreter from inside a existing virtual environment in Visual Studio code

I want to open an existing virtual environment in Visual Studio Code.我想在 Visual Studio Code 中打开一个现有的虚拟环境。 When I try to change the interpreter address inside of the settings.json (as instructed by https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments ) it doesn't work.当我尝试更改 settings.json 中的解释器地址(按照https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments 的指示)时,它不起作用。 With the virtual environment that I have, there is no python.exe file, I believe that is the issue as to why the following settings.json file doesn't work.在我拥有的虚拟环境中,没有 python.exe 文件,我相信这就是为什么以下 settings.json 文件不起作用的问题。

{
    "python.pythonPath": "D:\\GitProjects\\OrganizationApp\\venv1\\bin\\python"
}

First of all Try open the root folder which venv folder resides in with Visual Studio Code.首先尝试使用 Visual Studio Code 打开 venv 文件夹所在的根文件夹。 (If you are on Linux you can just got the directory and open terminal and type code ). (如果您使用的是 Linux,则只需获取目录并打开终端并输入code )。

You should get a pop up to change the interpreter.您应该会弹出一个更改解释器的窗口。

If not then Open your command palette Ctrl + Shift + P Search for Python interpreter and select the Venv one.如果没有,则打开命令面板Ctrl + Shift + P搜索 Python 解释器并选择 Venv 之一。

According to your description, you could refer to the following steps to check the creation and use of the virtual environment in VSCode:根据您的描述,您可以参考以下步骤检查VSCode中虚拟环境的创建和使用:

Prerequisites.先决条件。

  1. Check if python is available.检查python是否可用。

Enter cmd from the computer, enter the cmd window, enter python, and output the python version number to represent python is available.从电脑输入cmd,进入cmd窗口,输入python,输出python版本号,代表python可用。

在此处输入图片说明

If it is not available, please check the python installation package and check the python environment variables .如果没有,请查看python安装包,查看python环境变量

  1. The python extension was successfully installed in VSCode. VSCode 中成功安装了 python 扩展。

Create and use a virtual environment:创建和使用虚拟环境:

  1. Create a virtual environment in the vscode terminal.在 vscode 终端中创建一个虚拟环境。 python3 -m venv ./venv1 or python -m venv ./venv1 Reference: virtual environments . python3 -m venv ./venv1python -m venv ./venv1参考: 虚拟环境

  2. Select Yes when the prompt box pops up.弹出提示框选择Yes

    在此处输入图片说明

  3. Then the interpreter will automatically be replaced with the created virtual environment.然后解释器将自动替换为创建的虚拟环境。

    在此处输入图片说明

  4. After restarting VSCode, there is no python interpreter displayed in the lower left corner.重启VSCode后,左下角没有显示python解释器。 We can create or open a python file and it will automatically display the interpreter.我们可以创建或打开一个 python 文件,它会自动显示解释器。

  5. Click on the interpreter to choose other available interpreters.单击解释器以选择其他可用的解释器。

    在此处输入图片说明

  6. Use the shortcut key Ctrl+Shift+` to open a new terminal and enter the virtual environment.使用快捷键Ctrl+Shift+`打开一个新终端,进入虚拟环境。

    在此处输入图片说明

Update:更新:

When I open other projects or don't open any project in VSCode , the virtual environment created before is not displayed in the python interpreter options.当我在 VSCode 中打开其他项目或没有打开任何项目时,之前创建的虚拟环境不会显示在 python 解释器选项中。 The reason is that the virtual environment we created is based on the current project and it exists in this project.原因是我们创建的虚拟环境是基于当前项目的,它存在于这个项目中。 like this:像这样:

在此处输入图片说明

Although this virtual environment is not displayed in other projects, I can use this virtual environment by selecting the python.exe of the virtual environment ( 'enter interpreter path' ' .venv ' ' Scripts ').虽然这个虚拟环境在其他项目中没有显示,但是我可以通过选择虚拟环境的python.exe('输入解释器路径'' .venv '' Scripts ')来使用这个虚拟环境。 Therefore, if there is no python.exe , it is recommended to create a new virtual environment.因此,如果没有python.exe ,建议新建一个虚拟环境。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择虚拟环境 Python 解释器的 Visual Studio Code 问题 - Visual Studio Code issue selecting Virtual Environment Python interpreter 阻止 Visual Studio Code 激活 Python 虚拟环境 - Prevent Visual Studio Code from activating the Python virtual environment 如何在 Visual Studio Code 中为 Python 设置虚拟环境? - How can I set up a virtual environment for Python in Visual Studio Code? 使用Visual Studio Code在Python中设置虚拟环境 - Settings of Virtual Environment in Python with Visual Studio Code 如何将 python 解释器添加到 Visual Studio Code? - How to add python interpreter to Visual Studio Code? 我的 Visual Studio Code 没有使用我设置的 Python 解释器 - My Visual Studio Code is not using the Python interpreter that I have set it to 在 Visual Studio Code 中将 Python 包安装到虚拟环境中的问题 - Problems installing Python packages into a virtual environment in Visual Studio Code 如何在虚拟环境中使用 Python 解释器 - How to use a Python interpreter in a virtual environment 无法从 Visual Studio 代码解释器更改 python 版本 - can't change python version from visual studio code interpreter 如何在 Visual Studio Code 中将我的解释器从 Python 3.8 更改为 Python 3.9? [已解决] - How could I change my Interpreter from Python 3.8 to Python 3.9 in Visual Studio Code? [Resolved with answer]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM