如何在Visual Studio代码中的现有虚拟环境中设置Python解释器 [英] How to set a Python Interpreter from inside a existing virtual environment in Visual Studio code

查看:287
本文介绍了如何在Visual Studio代码中的现有虚拟环境中设置Python解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Visual Studio Code中打开一个现有的虚拟环境.当我尝试更改settings.json内部的解释器地址时(如 https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments )无效.在我拥有的虚拟环境中,没有python.exe文件,我认为这是为什么以下settings.json文件不起作用的问题.

I want to open an existing virtual environment in 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. 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.pythonPath": "D:\\GitProjects\\OrganizationApp\\venv1\\bin\\python"
}

推荐答案

根据您的描述,您可以参考以下步骤来检查VSCode中虚拟环境的创建和使用情况:

According to your description, you could refer to the following steps to check the creation and use of the virtual environment in VSCode:

先决条件.

  1. 检查python是否可用.

在计算机上输入cmd,进入cmd窗口,输入python,然后输出python版本号以表示python可用.

Enter cmd from the computer, enter the cmd window, enter python, and output the python version number to represent python is available.

如果不可用,请检查 python安装程序包并检查python 环境变量.

If it is not available, please check the python installation package and check the python environment variables.

  1. python扩展已成功安装在VSCode中.

创建和使用虚拟环境:

  1. 在vscode终端中创建虚拟环境. python3 -m venv ./venv1python -m venv ./venv1参考:虚拟环境.

在弹出提示框时选择Yes.

然后,解释器将自动替换为创建的虚拟环境.

Then the interpreter will automatically be replaced with the created virtual environment.

重新启动VSCode后,左下角没有显示python解释器.我们可以创建或打开一个python文件,它将自动显示解释器.

After restarting VSCode, there is no python interpreter displayed in the lower left corner. We can create or open a python file and it will automatically display the interpreter.

单击口译员以选择其他可用的口译员.

Click on the interpreter to choose other available interpreters.

使用快捷键Ctrl + Shift +`打开新终端并进入虚拟环境.

Use the shortcut key Ctrl+Shift+` to open a new terminal and enter the virtual environment.

更新:

当我在VSCode中打开其他项目或不打开任何项目时,之前创建的虚拟环境不会显示在python解释器选项中.原因是我们创建的虚拟环境基于当前项目,并且存在于该项目中.像这样:

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. The reason is that the virtual environment we created is based on the current project and it exists in this project. like this:

尽管此虚拟环境未在其他项目中显示,但我可以通过选择虚拟环境的python.exe(输入解释器路径",".venv","Scripts")来使用此虚拟环境.因此,如果没有python.exe,建议创建一个新的虚拟环境.

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'). Therefore, if there is no python.exe, it is recommended to create a new virtual environment.

这篇关于如何在Visual Studio代码中的现有虚拟环境中设置Python解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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