在 VisualStudio 代码中使用蟒蛇

我在这里有点困惑,最新的 Anaconda 发行版,2018.12在撰写本文时提供了一个安装 Microsoft Visual Studio Code 的选项,这很棒。

在启动 VSC 和启动 Python: Select Interpreter之后以及安装了新的 Anaconda 之后,我可以看到 ~Anaconda3\python.exe,我假设它是 Anaconda Python 环境,然而,当我尝试运行一些命令时,我得到:

PS ~\Documents\Python Scripts\vs> ~/Anaconda3/Scripts/activate
PS ~\Documents\Python Scripts\vs> conda activate base

Conda: 术语“ conda”不被认为是 cmdlet 的名称, 函数、脚本文件或可操作程序。请检查 名称,或者如果包含路径,则验证路径是否正确,并 再试一次,在行: 1 char: 1

现在我知道它可能与环境变量有关,但是我发现它非常奇怪,因为在 Anaconda 安装期间,特别提到了不需要向环境变量添加 Anaconda 路径。但是在发生错误之后,集成终端设法启动 Python,我就能够运行代码了。

下一个问题是,在运行一个简单的脚本之后,我无法查看调试器中的任何变量,如教程 给你所示:

msg = "Hello World"
print(msg)

我希望看到类似的结果,如在链接中显示的 dunder 变量,我也更新了我的 launch.jsonstopOnEntry = True下面的步骤。

我想知道是否有可能使用 Visual Studio Code 和 Anaconda 作为解释器,而不需要从原始发行版中注册变量,以及我是否遗漏了任何必要的东西。

我期待的经验,更直接,但也可能是我错过了什么,我运行在 视窗10。

160922 次浏览

Activating a conda environment does not place conda on your PATH. You need to launch the Anaconda Prompt app from your Start menu to get a command-line with conda on your PATH if you didn't check the box to include conda during installation.

Also realize that conda only supports PowerShell as of conda 4.6 which was released in January 2019.

And the Python extension for VS Code works with conda fine. Create a conda environment and the extension will allow you to select it as your environment/interpreter.

For me, this solution worked in VSC (1.40) ->

1.Set the Interpreter to Python 3.7.1 ('base':conda)

2. Rather than using PowerShell I switched (Select Default Shell) to Command Prompt and started a New Terminal -> now it's in conda (base) environment.

[for Anaconda Distribution, 2018.12]

For me, this article solved my issue (on Windows 10): Specifically, the settings.json entry: terminal.integrated.shellArgs.windows e.g.:

    "terminal.integrated.shellArgs.windows": [
"/K",
"C:\\Programs\\Anaconda3\\Scripts\\activate.bat & conda activate py37"
]

It works well with command line but doesn't seem to work with PowerShell.

Adding this setting to settings.json solved for me:

"terminal.integrated.shellArgs.windows": [
"PowerShell -NoExit -File C:\\ProgramFiles\\Anaconda\\shell\\condabin\\conda-hook.ps1"
]

PS: This answer is based on kenlukas's answer, but for powershell instead of cmd.

If you launch VS Code application from inside the Anaconda Navigator application, you automatically get a "conda-aware" VSCode integrated terminal.

This way you don't need to change the settings in VS Code itself.

I beat my head on this for far too long... launching VS Code from an Anaconda Prompt both feels clunky and didn't really work for integrated Powershell terminals (the default VS Code integrated terminal on Windows)

I wanted to be able to launch VS Code from any prompt (and usually the Windows menu shortcut) and still interact with Conda.

These two methods both worked for Anaconda 2020.02, VS Code 1.44.0, on Windows 10.

Easy & Quick

Switch VS Code to use cmd.exe as the default integrated terminal shell by:

  1. opening the command palette (Control-Shift-P)
  2. search for Terminal: Select Default Profile
  3. select Command Prompt

Harder / Powershell

  1. add the location of conda to your PATH (if you did not add it via the installer). For me on an "All Users" install this is C:\ProgramData\Anaconda\Scripts
  2. from an Administrator Powershell prompt change the Powershell Execution Policy to remote signed i.e. Set-ExecutionPolicy RemoteSigned, or from a non-admin Powershell prompt change the Powershell Execution Policy to remote signed i.e. Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
  3. open an Anaconda Prompt and run conda init powershell which will add Conda related startup to a Powershell profile.ps1 somewhere in your user's profile.

When you run VS Code and either debug a .py or launch a Powershell integrated terminal you should now see a message about "Loading personal and system profiles" and the Conda environment being prefixed on the terminal's prompt.

Following the trails of @andre-barbosa and @kenlukas, the below configuration works well for me (PowerShell on Windows 10):

"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy",
"ByPass",
"-NoExit",
"-Command",
"your-path-to-\\conda-hook.ps1",
";conda activate 'your-path-to-the-base-conda-environment'"
]

Substitute "your-path-to-\conda-hook.ps1" and "your-path-to-the-base-conda-environment" with your actual path, and add it to VSCode's settings.json. Check out the properties of your "Anaconda Powershell Prompt" shortcut and you'll find what you need.

The key is really to obey the rules of VSCode's JSON configuration file, by turning the whole arguments string into a comma separated list.

To set anaconda prompt as your default terminal in VScode:

  1. (type) CTRL + SHIFT + P
  2. (search for:) open settings
  3. (click:) Preferences: Open Settings (JSON)

Then add three line configuration:

{
... # any other settings you have already added (remove this line)


"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K", "C:\\Anaconda3\\Scripts\\activate.bat C:\\Anaconda3"],
"python.condaPath": "C:\\Anaconda3\\Scripts\\conda.exe"
}

Finally, Restart your VScode

I am working on VS Version: 1.47.0 (user setup). I recently switched from Jupyter Notebook (still working there for my reports) to VS Code. But my codes were not running. Same problem of 'conda' is not recognized. What worked for me was:

  1. Kill Terminal and start afresh
  2. In the terminal dropdown option, selected "Select Default Shell"
  3. Changed that from powershell to CMD.
  4. Now all my preinstalled libraries and conda working perfectly fine.

Easiest way is to open anaconda shell prompt, then launch VScode from that terminal. Now VScode can see everthing from anaconda!

Open file C:\Users\YOUR-USER-NAME\AppData\Roaming\Code\User\settings.json

Insert section "Miniconda3": { }

"terminal.integrated.profiles.windows": {


"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},


"Miniconda3": {
"source": "PowerShell",
"args": "-ExecutionPolicy ByPass -NoExit -Command \"& 'C:\\ProgramData\\Miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\ProgramData\\Miniconda3' \"",
"icon": "C:\\ProgramData\\Miniconda3\\Lib\\site-packages\\conda\\shell\\conda_icon.ico"
},


"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},


"Git Bash": {
"source": "Git Bash"
}


}

No need to touch env:PATH

Btw, Miniconda3.args is copied from Anaconda Powershell Prompt (Miniconda3) -> Properties.Shortcut.Target

If you are using Anaconda instead of Miniconda, you may want to modify a few paths.

VS Code is just an editor, Python Extension keeps breaking stuff, and spreads its config files everywhere. Stopped using long time ago.

As many have pointed out, you can simply launch "code" from the Anaconda Command Prompt (ACP).

The ACP is simply a shortcut to a Command Prompt (and equivalently on powershell) with some arguments. (e.g. %windir%\System32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3)

As @janh pointed out, it is annoying to launch vscode from there each time, instead, you can change the default within the VS Code setting.json file, as @zonhgyi pointed out, but what he recommended is/will be deprecated.

REF: https://code.visualstudio.com/docs/editor/integrated-terminal#_configuration

Here's what I ended up using. No need to change PATH, no need to make other changes than editing the settings.json file (which can be accessed through Ctrl+Shift+P -> settings.json)

...


"terminal.integrated.profiles.windows": {
"Conda": {
"path": "C:\\windows\\System32\\cmd.exe",
"args": [
"/K", "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat C:\\ProgramData\\Anaconda3"
],
"icon": "squirrel"
}
},
"terminal.integrated.defaultProfile.windows": "Conda",
...

Firstly, you have to add 4~5 path in environment variable

enter image description here

somebody may not have .../usr/bin. it's not important.

and secondly, you have to change default profile to command prompt in vs code. Using default shell or power shell would have some problem. in my case , I can't activate conda environment which I created. it keep stop in the (base) env of anaconda. press ctrl+shift+p select terminal:select default profile and click. Then choose command prompt. when changing into command prompt, I can use activate (myenvname) to other env.

enter image description here

Command Prompt (default) stopped working for me Sept 2021

Originally I used the quick fix that many have suggested:

Terminal dropdown --> Select default profile --> Command Prompt

Not as good as PowerShell but like many of you I didn't care and just wanted to get on with my project.

Recently, VS Code updated and despite the above setting, PowerShell would run by default in terminal. Back to square one.

The one-line fix

I found another post and tried the following in my Anaconda PowerShell Prompt (Miniconda3):

conda init

which changed several paths, including some used by PowerShell. Now Python scripts running in VS Code run in PowerShell because PowerShell now recognises Conda.

Note

If you've created a virtual environment you still need to indicate this in VS Code:

View --> Command Palette... --> Python: Select Interpreter -->

~\Path\to\my\virtual\environment\python.exe

The "terminal.integrated.shellArgs.windows" method is depreciated:

This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#.

After reading the VS code docs (configuring profiles), I created a new profile for the Anaconda Prompt. The following is what is in my settings.json file:

"terminal.integrated.profiles.windows":{
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
},
"Anaconda Prompt": {
"source": "PowerShell",
"args": [
"powershell",
"-NoExit",
"-ExecutionPolicy ByPass",
"-NoProfile",
"-File C:\\path\\to\\Miniconda3\\shell\\condabin\\conda-hook.ps1"
],
"icon": "smiley"
}
}
  1. Click control+shift+p

  2. Search Teminal: Select Default profile

  3. Open new terminal (python recent virtual environment)

  4. Again click control+shift+p

  5. Search Python: Select Interpreter (choose your environment or base conda environment)

  6. Open new terminal (conda virtual environment)

I think this is a better way, to let conda work with powershell in vscode, without change your PATH, which is unrecommanded:

  1. win + X, open terminal as Administrator.
  2. run C:\Users\xxx\anaconda3\Scripts\conda.exe init powershell, you can use where conda in Anaconda Prompt (anaconda3) to find the path.

It works for me. May be run conda.exe init powershell in Anaconda Prompt (anaconda3) directly is also helpful, but I haven't try it.

This worked for me:

C:\Users\xxx\Anaconda3\Scripts C:\Users\xxx\Anaconda3 C:\Users\xxx\Anaconda3\Library\bin

Just add these three path to your Path

For me my interpreter was set to a different version:

CTRL+SHIFT+P -> Select Python: Select Interpreter

Select the latest version.