如何在 VS Code 中为 Python 设置虚拟环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54106071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:26:05  来源:igfitidea点击:

How to setup virtual environment for Python in VS Code?

pythonvisual-studio-code

提问by Hrvoje T

In my project folder I created venv folder.

在我的项目文件夹中,我创建了 venv 文件夹。

python -m venv venv

When I in VS Code run command select python interpretermy venv folder is not shown. I went one level up like suggested herebut VS Code doesn't see my virtual interpreter.

当我在 VS Code 中运行命令时,select python interpreter我的 venv 文件夹没有显示。我像这里建议的那样上一级,但 VS Code 没有看到我的虚拟解释器。

What did I miss?

我错过了什么?

回答by Sam

I almost run into same problem everytime I am working on VS-Code using venv. I follow below steps, hope it helps:

每次我使用 venv 处理 VS-Code 时,我几乎都会遇到同样的问题。我按照以下步骤操作,希望对您有所帮助:

  1. Go to File > preferences > Settings.
  2. Click on Workspace settings.
  3. Under Files:Association, you will find Edit in settings.json, click on that.
  4. Update "python.pythonPath": "Your_venv_path/bin/python"under workspace settings. (For Windows): Update "python.pythonPath": "Your_venv_path/Scripts/python.exe"under workspace settings.

  5. Restart VSCode incase if it still doesn't show your venv.

  1. File > preferences > Settings
  2. 单击Workspace settings
  3. 在 下Files:Association,您会找到Edit in settings.json,单击它。
  4. "python.pythonPath": "Your_venv_path/bin/python"在工作区设置下更新。(对于 Windows):"python.pythonPath": "Your_venv_path/Scripts/python.exe"在工作区设置下更新。

  5. 如果仍然没有显示您的 venv,请重新启动 VSCode。

UPDATE:

更新:

-I have been using vs code for a while now and found an another way to show virtual environments in vs code.

- 我已经使用 vs 代码一段时间了,并找到了另一种在 vs 代码中显示虚拟环境的方法。

  • Go to the parent folder in which venvis there through command prompt.
  • Type code .and Enter. [Working on both windows and linux for me.]
  • That should also show the virtual environments present in that folder.
  • venv通过命令提示符转到其中的父文件夹。
  • 键入code .并输入。[为我在 windows 和 linux 上工作。]
  • 这也应该显示该文件夹中存在的虚拟环境。

回答by Neil

With a newer VS Code version it's quite simple.

使用较新的 VS Code 版本非常简单。

Open VS Code in your project's folder.

在项目文件夹中打开 VS Code。

Then open Python Terminal(Ctrl-Shift-P: Python: Create Terminal)

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

In the terminal:

在终端:

python -m venv .venv

you'll then see the following dialog: enter image description here

然后你会看到以下对话框: 在此处输入图片说明

click Yes

点击 Yes

Then Python: Select Interpreter(via Ctrl-Shift-P)

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

and select the option (in my case towards the bottom)

并选择选项(在我的情况下朝向底部)

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

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

If you see

如果你看到

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

Activate.ps1 没有数字签名。您无法在当前系统上运行此脚本。

you'll need to do the following: https://stackoverflow.com/a/18713789/2705777

您需要执行以下操作:https: //stackoverflow.com/a/18713789/2705777

For more information see: https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments

有关更多信息,请参阅:https: //code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments

回答by The Welsh Dragon

I was having the same issue until I worked out that I was trying to make my project directory and the virtual environment one and the same - which isn't correct.

我遇到了同样的问题,直到我发现我试图使我的项目目录和虚拟环境相同 - 这是不正确的。

I have a \Code\Pythondirectory where I store all my Python projects. My Python 3 installation is on my Path.

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

If I want to create a new Python project (Project1) with its own virtual environment, then I do this:

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

python -m venv Code\Python\Project1\venv

Then, simply opening the folder (Project1) in Visual Studio Code ensures that the correct virtual environment is used.

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

回答by Prashanth Pradeep

I fixed the issue without changing the python path as that did not seem like the right solution for me. The following solution worked for me, hopefully it works for you as well :))

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

  1. Open cmd in windows / shell in Linux/Mac.
  2. Activate your virtualenv (using source activate / activate.bat / activate.ps1 if using power shell)

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

  3. Navigate to your project directory and open vscode there.

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

  4. in VS Code, goto File --> Preferences --> Settings (dont worry you dont need to open the json file)

  5. In the setting search bar search for virtual / venv and hit enter. You should find the below in the search bar:

  1. 在 Linux/Mac 的 windows/shell 中打开 cmd。
  2. 激活您的 virtualenv(如果使用电源外壳,请使用 source activate / activate.bat / activate.ps1)

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

  3. 导航到您的项目目录并在那里打开 vscode。

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

  4. 在 VS Code 中,转到文件 --> 首选项 --> 设置(不用担心您不需要打开 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).

Python: Venv Folders 在你的主目录中查找虚拟环境的文件夹(默认支持 pyenv、direnv 和 virtualenvwrapper)。

  1. Add item, and then enter the path of the scripts of your virtuanenv which has the activate file in it. For example in my system, it is:

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

  2. Save it and restart VS Code.

  3. To restart, open cmd again, navigate to your project path and open vs code. (Note that your venv should be activated in cmd before you open vs code from cmd)

  1. 添加项目,然后输入其中包含激活文件的 virtuanenv 脚本的路径。例如在我的系统中,它是:

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

  2. 保存并重新启动 VS Code。

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

Command to open vs code from cmd:

code .

从 cmd 打开 vs 代码的命令:

code .

How to setup virtualenv in VS code

如何在 VS 代码中设置 virtualenv

回答by Carmine Tambascia

This is an adding to @Sam answer that though is correct is missing the fact that anytime you open a folder in visual studio code, it create a .vscode folder, but those can be multiple, created any time you eventually open a directory. The .vscode folder has JSON objects that content properties such "setting.json", in which one declare the Interpreter to use at that ".vscode" level( refer to this for more clarifications What is a 'workspace' in VS Code?).

这是对@Sam 答案的补充,尽管它是正确的,但缺少这样一个事实:无论何时您在 Visual Studio 代码中打开一个文件夹,它都会创建一个 .vscode 文件夹,但这些文件夹可以是多个,在您最终打开目录时创建。.vscode 文件夹具有包含“setting.json”等属性的 JSON 对象,其中声明解释器在该“.vscode”级别使用(有关更多说明,请参阅此内容什么是 VS Code 中的“工作区”?) .

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

So potentially you could open VS code at another level in the virtual Env, it create another .vscode folder that assume as Python directory those of the global machine and so having such error, and has I experienced has nothing to do if the Virtual Env is activated or not.

因此,您可能可以在虚拟环境中的另一个级别打开 VS 代码,它会创建另一个 .vscode 文件夹,该文件夹假定为全局机器的 Python 目录,因此出现此类错误,如果虚拟环境是,我是否经历过无关紧要激活与否。

This indeed what happened to me, I have indeed a DjangoRESTAPI_GEN folder in which I initially opened the IDE and it did recognize the Virtual Env Python path, the a few days after I opened it at the level where git is, so it did created another .vscode, that picked the global Python Interpreter, causing my lint in the Virtual Environment not been used, and the virtual env interpreter not even showed in "select python interpreter". But as wrote opening the IDE at the level where the .vscode that has the settings.json with correct path, it does.

这确实发生在我身上,我确实有一个 DjangoRESTAPI_GEN 文件夹,我最初在其中打开了 IDE,它确实识别了 Virtual Env Python 路径,几天后我在 git 所在的级别打开它,所以它确实创建了另一个.vscode,它选择了全局 Python 解释器,导致我在虚拟环境中的 lint 没有被使用,虚拟环境解释器甚至没有显示在“选择 python 解释器”中。但是正如所写的那样,在具有正确路径的 settings.json 的 .vscode 级别打开 IDE,它确实如此。

Once you set the correct path in the setting.json and select the virtual env interpreter, then VS Code will automatically activate the VE in its terminal

在 setting.json 中设置正确的路径并选择虚拟环境解释器后,VS Code 将自动在其终端中激活 VE

enter image description here

在此处输入图片说明

回答by Alex Telon

Many have mentioned the python.pythonPathmethod.

很多人都提到了这个python.pythonPath方法。

Another way is adding a envFilein the launch.jsonlike this:

另一种方式是添加envFilelaunch.json这样的:

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

回答by HectorIX

Have you activated your environment? Also you could try this: vscode select venv

你有没有激活你的环境?你也可以试试这个: vscode select venv

回答by julianvare

I had the same problem and it was because PowerShell was not updated. Sometimes Windows preserve version 2.* and I had to manually download and install version 3. After that problem solved and I could use virtual environments very well.

我遇到了同样的问题,这是因为 PowerShell 未更新。有时 Windows 会保留版本 2.*,我不得不手动下载并安装版本 3。问题解决后,我可以很好地使用虚拟环境。

回答by Rami Ma

I had the same problem and the solution was pretty easy:

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

"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."

“如果你在 VS Code 运行时新建一个 conda 环境,使用 Reload Window 命令刷新 Python 显示的环境列表:选择 Interpreter;否则你可能看不到那里的环境。它可能需要很短的时间才能出现;如果您一开始看不到它,请等待 15 秒,然后再次尝试使用该命令。”

That's written on Vscode site

这是写在Vscode 网站上的

Note: to Reload Window: ctrl+shift+p in Vscode, then write reload window

注意:要重新加载窗口:在 Vscode 中按 ctrl+shift+p,然后编写重新加载窗口

回答by Mark Kortink

There is a VSCode extension called "Python Auto Venv" that automatically detects and uses your virtual environment if there is one.

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