Activating Anaconda Environment in VsCode

后端 未结 10 1021
囚心锁ツ
囚心锁ツ 2020-11-30 22:32

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?

相关标签:
10条回答
  • 2020-11-30 22:44

    Just launch the VS Code from the Anaconda Navigator. It works for me.

    0 讨论(0)
  • 2020-11-30 22:49

    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.

    0 讨论(0)
  • 2020-11-30 22:50

    Although approved answer is 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:

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

    0 讨论(0)
  • 2020-11-30 22:53

    Find a note here: 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:

     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.

    0 讨论(0)
  • 2020-11-30 22:57

    Simply use

    1. shift + cmd + P
    2. Search Select Interpreter

    1. Select it and it will show you the list of your virtual environment created via conda and other python versions

    1. select the environment and you are ready to go.

    Quoting the 'Select and activate an environment' docs

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

    0 讨论(0)
  • 2020-11-30 22:59

    The best option I found is to set the python.venvPath parameter in vscode settings to your anaconda envs folder.

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

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

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

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

    0 讨论(0)
提交回复
热议问题