Python 在 VsCode 中激活 Anaconda 环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43351596/
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 22:55:02  来源:igfitidea点击:

Activating Anaconda Environment in VsCode

pythonvisual-studio-codeanaconda

提问by ctrlalt313373

I have Anaconda working on my system and VsCode working, but how do I get VsCode to activate a specific environment when running my python script?

我有 Anaconda 在我的系统上工作并且 VsCode 工作,但是如何在运行我的 python 脚本时让 VsCode 激活特定环境?

采纳答案by eric_camplin

If Anaconda is your default Python install then it just works if you install the Microsoft Python extension.

如果 Anaconda 是您的默认 Python 安装,那么它只在您安装Microsoft Python 扩展时才有效

The following should work regardless of Python editor or if you need to point to a specific install:

无论 Python 编辑器如何,或者您是否需要指向特定安装,以下内容都应该有效:

In settings.json edit python.path with something like

在 settings.json 中编辑 python.path 类似

"python.pythonPath": "C:\Anaconda3\envs\py34\python.exe"

Instructionsto edit settings.json

编辑 settings.json 的说明

回答by Vaibhav K

Simply use

只需使用

  1. shift + cmd + P
  2. Search Select Interpreter
  1. Shift + cmd + P
  2. 搜索选择口译员

pyhton : Select Interpreter

pyhton:选择解释器

  1. Select it and it will show you the list of your virtual environment created via conda and other python versions
  1. 选择它,它将显示通过 conda 和其他 python 版本创建的虚拟环境列表

Activating conda virtual environment

激活 conda 虚拟环境

  1. select the environment and you are ready to go.
  1. 选择环境,您就可以开始了。

Quoting the 'Select and activate an environment' docs

引用“选择并激活环境”文档

Selecting an interpreter from the list adds an entry for python.pythonPathwith
the path to the interpreter inside your Workspace Settings.

从列表中选择一个解释器会添加一个条目,python.pythonPath其中
包含工作区设置中解释器的路径。

回答by alexbhandari

The best option I found is to set the python.venvPathparameter in vscode settingsto your anaconda envs folder.

我发现的最佳选择是将vscode 设置中python.venvPath参数设置为您的 anaconda envs 文件夹。

"python.venvPath": "/Users/[...]/Anaconda3/envs"

Then if you bring up the command palette (ctl + shift + Pon windows/linux, cmd + shift + Pon mac) and type Python: Select Workspace Interpreterall your envs will show up and you can select which env to use.

然后,如果您调出命令面板(ctl + shift + P在 windows/linux 上,cmd + shift + P在 mac 上)并键入Python: Select Workspace Interpreter所有 envs 将显示,您可以选择要使用的 env。

The python extensionwill also need to be installed for the Select Workspace Interpreter option.

Python扩展也将需要安装的选择工作区翻译选项。

Note: The Select Workspace Interpreter takes around 10 seconds to come up on my computer using the current version of VSCode.

注意:使用当前版本的 VSCode,Select Workspace Interpreter 需要大约 10 秒才能在我的计算机上出现。

回答by Zhenyu

Setting python.pythonPathin VSCode's settings.jsonfile doesn't work for me, but another method does. According to the Anaconda documentation at Microsoft Visual Studio Code (VS Code):

python.pythonPath在 VSCode 的settings.json文件中设置对我不起作用,但另一种方法可以。根据Microsoft Visual Studio Code (VS Code)的 Anaconda 文档:

When you launch VS Code from Navigator, VS Code is configured to use the Python interpreter in the currently selected environment.

当您从 Navigator 启动 VS Code 时,VS Code 被配置为在当前选择的环境中使用 Python 解释器。

Anaconda Navigator

蟒蛇导航器

回答by pkowalczyk

Although approved answeris correct, I want to show a bit different approach (based on this answer).

虽然批准的答案是正确的,但我想展示一些不同的方法(基于这个答案)。

Vscode can automatically choose correct anaconda environment if you start vscode from it. Just add to user/workspace settings:

如果您从中启动 vscode,vscode 可以自动选择正确的 anaconda 环境。只需添加到用户/工作区设置:

{
    "python.pythonPath": "C:/<proper anaconda path>/Anaconda3/envs/${env:CONDA_DEFAULT_ENV}/python"
}

It works on Windows, macOS and probably Unix. Further read on variable substitution in vscode: here.

它适用于 Windows、macOS 和 Unix。进一步阅读 vscode 中的变量替换:这里

回答by Chris Fonnesbeck

Unfortunately, this does not work on macOS. Despite the fact that I have export CONDA_DEFAULT_ENV='$HOME/anaconda3/envs/dev'in my .zshrc and "python.pythonPath": "${env.CONDA_DEFAULT_ENV}/bin/python", in my VSCode prefs, the built-in terminal does not use that environment's Python, even if I have started VSCode from the command line where that variable is set.

不幸的是,这不适用于 macOS。尽管export CONDA_DEFAULT_ENV='$HOME/anaconda3/envs/dev'"python.pythonPath": "${env.CONDA_DEFAULT_ENV}/bin/python", 在我的 .zshrc和我的 VSCode 首选项中都有,但内置终端不使用该环境的 Python,即使我已经从设置了该变量的命令行启动了 VSCode。

回答by Jason Ridenour

If you need an independent environment for your project: Install your environment to your project folder using the --prefix option:

如果您的项目需要一个独立的环境:使用 --prefix 选项将您的环境安装到您的项目文件夹中:

conda create --prefix C:\your\workspace\root\awesomeEnv\ python=3

In VSCode launch.json configuration set your "pythonPath" to:

在 VSCode launch.json 配置中,将“pythonPath”设置为:

"pythonPath":"${workspaceRoot}/awesomeEnv/python.exe"

回答by redice li

Find a note here: https://code.visualstudio.com/docs/python/environments#_conda-environments

在此处查找注释:https: //code.visualstudio.com/docs/python/environments#_conda-environments

As noted earlier, the Python extension automatically detects existing conda environments provided that the environment contains a Python interpreter. For example, the following command creates a conda environment with the Python 3.4 interpreter and several libraries, which VS Code then shows in the list of available interpreters:

如前所述,Python 扩展会自动检测现有的 conda 环境,前提是该环境包含 Python 解释器。例如,以下命令使用 Python 3.4 解释器和几个库创建 conda 环境,然后 VS Code 将显示在可用解释器列表中:

 conda create -n env-01 python=3.4 scipy=0.15.0 astroid babel 

In contrast, if you fail to specify an interpreter, as with conda create --name env-00, the environment won't appear in the list.

相反,如果您没有指定解释器,如 conda create --name env-00,环境将不会出现在列表中。

回答by Evil Dr. PorkChop

I found a hacky solution replace your environment variable for the original python file so instead it can just call from the python.exe from your anaconda folder, so when you reference python it will reference anaconda's python.

我发现一个hacky解决方案替换了原始python文件的环境变量,因此它可以从anaconda文件夹中的python.exe调用,因此当您引用python时,它将引用anaconda的python。

So your only python path in env var should be like:

所以你在 env var 中唯一的 python 路径应该是这样的:

"C:\Anaconda3\envs\py34\", or wherever the python executable lives

If you need more details I don't mind explaining. :)

如果您需要更多详细信息,我不介意解释。:)

回答by Mohammad Heydari

Just lunch the VS Codefrom the Anaconda Navigator. It works for me.

午饭的VS Code距离Anaconda Navigator。这个对我有用。