简体   繁体   English

启动 VSCode 时自动激活 conda 环境

[英]Activate conda enviroment automatically when starting VSCode

Setting环境

Suppose that I got a project called my_project that uses a python interpreter from a specific conda environment called my_env .假设我有一个名为my_project的项目,它使用来自名为my_env的特定my_env环境的 python 解释器。 When I set the interpreter of the project I will get the following settings.json file inside the .vscode directory of my_project :当我设置项目的解释器时,我将在my_project.vscode目录中获得以下 settings.json 文件:

{
    "python.pythonPath": "<path to conda>/envs/my_env/bin/python"
}

Problem问题

With this configuration I would expect VSCode to automatically activate my_env in the terminal everytime I open my_project .通过这种配置,我希望每次打开my_project my_env在终端中自动激活my_env But typically when I open my_project only the base environment of anaconda is activated in the terminal.但通常当我打开my_project只有 anaconda 的base环境在终端中被激活。 To activate my_env I got different options that are all manual.为了激活my_env我得到了不同的手动选项。

  1. conda activate my_env
  2. Press strg+shift+p and execute create new integrated terminalstrg+shift+p并执行create new integrated terminal
  3. Start a debug session启动调试会话

Probably there are even more ways of doing it.可能还有更多的方法可以做到这一点。

Question

How can I avoid this stuff such that VSCode simply activates my_env automatically inside the integrated terminal when I open my_project .如何避免这种情况,以便 VSCode 在打开my_project时在集成终端内自动激活my_env

Further Informations更多信息

  • OS: Linux操作系统:Linux
  • VSCode: 1.36 VSCode:1.36
  • Conda: 4.7.10康达:4.7.10

Another way out would be to create shortcut for activation.For eg in ~/.bashrc do the following另一种方法是创建激活快捷方式。例如在 ~/.bashrc 中执行以下操作

alias a="conda activate my_env"
alias d="conda deactivate"

So now you need to just type 'a' to activate and 'd' to deactivate.所以现在你只需要输入'a'来激活,输入'd'来停用。

The Python extension for VS Code will pick up and support conda environment as long as you have specified a Python version when you created your conda environment and you gave it a name (see the docs on environments ).只要您在创建 conda 环境时指定了 Python 版本并为其命名(请参阅有关环境文档),VS Code 的 Python 扩展就会选择并支持 conda 环境。

And one way to work around this if you can't create an environment as required above is to use conda run which will handle the activation for you when running your code.如果您无法按照上述要求创建环境,则解决此问题的一种方法是使用conda run ,它将在运行代码时为您处理激活。

  1. Create an workspace folder创建工作区文件夹
  2. Inside the create file post the following code:在创建文件中发布以下代码:
 { "folders": [ { "path": "." } ], "settings": { "python.condaPath": "/home/pcuser/anaconda3/condabin/conda", "python.pythonPath": "/home/pcuser/anaconda3/envs/my_env/bin/python", "python.terminal.activateEnvironment": true, "python.terminal.activateEnvInCurrentTerminal": true, } }
  1. Save configuration and close vscode保存配置并关闭 vscode
  2. Open vscode.打开 vscode。

It will select automatically the conda enviroment (Should appear at the bottom left of vscode).它将自动选择 conda 环境(应该出现在 vscode 的左下角)。 Inside terminal 'my_env' will also be selected.内部终端 'my_env' 也将被选中。 If vscode doesn't select the 'Python Selected Interpreter' before you open the terminal the my_env inside terminal won't be selected.如果 vscode 在打开终端之前没有选择“Python Selected Interpreter”,则不会选择终端内的 my_env。 Open the terminal after the my_env is selected by vscode vscode选中my_env后打开终端

I do have faced similar problem when I have multiple python project having virtual environments in system.当我在系统中有多个具有虚拟环境的 python 项目时,我确实遇到过类似的问题。

Since I was switching project based on workload, its difficult to activate right environment for specific project.由于我是根据工作量切换项目,因此很难为特定项目激活正确的环境。

As VSCODE contain settings.py which store python path to use in particular project.由于 VSCODE 包含 settings.py,它存储要在特定项目中使用的 python 路径。 But was not activating automatically.但是没有自动激活。

I followed below step and its very fast compared to other method I searched.我遵循了以下步骤,与我搜索的其他方法相比,它非常快。

Step 1. Delete current terminal.步骤 1. 删除当前终端。 You will see bin icon as in below pic.您将看到如下图所示的 bin 图标。

在此处输入图片说明

Step 2. Now start new terminal by shortcut.步骤 2. 现在通过快捷方式启动新终端。

CTRL + ` CTRL + `

You will see VSCODE auto detect python path settings and activate the environment.您将看到 VSCODE 自动检测 python 路径设置并激活环境。

Simple简单的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM