Python 在 Visual Studio Code 中使用 Anaconda
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54828713/
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
Working with Anaconda in Visual Studio Code
提问by BernardL
I am getting a bit confused here, the latest Anaconda Distribution, 2018.12 at time of writing comes with an option to install Microsoft Visual Studio Code, which is great.
我在这里有点困惑,在撰写本文时,最新的 Anaconda Distribution 2018.12 带有安装 Microsoft Visual Studio Code 的选项,这很棒。
When launching VSC and after Python: Select Interpreter
and with a fresh install of Anaconda, I can see ~Anaconda3\python.exe
which I assume is the Anaconda Python Environment, however, when I try to run some commands, I am getting:
在启动 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 : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1
conda :术语“conda”不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在行:1 字符:1
Now I know that it might be related to the environment variables but I find it highly odd as during the Anaconda installation, there are specific mentions that it is not required to add the Anaconda path to the environment variables. However after the error, the integrated terminal manages to launch Python and I am able to run code.
现在我知道它可能与环境变量有关,但我发现它很奇怪,因为在 Anaconda 安装期间,有特别提到不需要将 Anaconda 路径添加到环境变量中。然而,在错误发生后,集成终端设法启动 Python,我能够运行代码。
Next in line is that I am unable to view any variables in the debugger after running a simple script, as shown in the tutorial here:
接下来是,我无法运行一个简单的脚本后,查看在调试器中的任何变量,如在本教程在这里:
msg = "Hello World"
print(msg)
I do expect to see similar results as shown in the link such as the dunder variables, I have also updated my launch.json
with stopOnEntry = True
following the steps.
我确实希望看到链接中显示的类似结果,例如 dunder 变量,我还launch.json
按照stopOnEntry = True
以下步骤更新了我的结果。
I would like to know if it is possible to use Visual Studio Code with Anaconda as a interpreter without registering variables from the original distribution and if I am missing out anything required.
我想知道是否可以在不注册原始发行版中的变量的情况下将 Visual Studio Code 与 Anaconda 一起用作解释器,以及是否遗漏了所需的任何内容。
I expected the experience to be more straight forward but also I might be missing something, I am running on Windows 10.
我希望体验更直接,但我也可能会遗漏一些东西,我在Windows 10上运行。
采纳答案by Brett Cannon
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.
激活 conda 环境不会conda
放在您的PATH
. 您需要启动Anaconda Prompt
从开始菜单的应用程序来获得一个命令行用conda
你的PATH
,如果你没有检查框包含conda
安装过程中。
Also realize that conda
only supports PowerShell as of conda
4.6 which was released in January 2019.
另请注意,conda
仅支持conda
2019 年 1 月发布的4.6 版PowerShell 。
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.
VS Code 的 Python 扩展可以正常工作conda
。创建一个conda
环境,扩展程序将允许您选择它作为您的环境/解释器。
回答by Spanish_Inquisition
For me, this solution worked in VSC (1.40) ->
对我来说,这个解决方案适用于 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]
1. 将解释器设置为 Python 3.7.1 ('base':conda)
2. 我没有使用 PowerShell,而是将(选择默认 Shell)切换到命令提示符并启动了一个新终端 -> 现在它处于 conda(基础)环境中。
[Anaconda Distribution, 2018.12]
回答by janh
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)
我在这方面犹豫了太久……从 Anaconda Prompt 启动 VS Code 既感觉笨重又不适用于集成的 Powershell 终端(Windows 上的默认 VS Code 集成终端)
I wanted to be able to launch VS Code from any prompt (and usually the Windows menu shortcut) and still interact with Conda.
我希望能够从任何提示(通常是 Windows 菜单快捷方式)启动 VS Code 并且仍然与 Conda 交互。
These two methods both worked for Anaconda 2020.02, VS Code 1.44.0, on Windows 10.
这两种方法都适用于 Windows 10 上的 Anaconda 2020.02、VS Code 1.44.0。
Easy & Quick
简单快捷
Switch VS Code to use cmd.exe as the default integrated terminal shell by:
通过以下方式切换 VS Code 以使用 cmd.exe 作为默认的集成终端 shell:
- opening the command palette (
Control-Shift-P
) - search for
Terminal: Select Default Shell
- select
Command Prompt
- 打开命令面板 (
Control-Shift-P
) - 搜索
Terminal: Select Default Shell
- 选择
Command Prompt
Harder / Powershell
更难/ Powershell
- 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 isC:\ProgramData\Anaconda\Scripts
- from an Administrator Powershell prompt change the Powershell Execution Policy to remote signed i.e.
Set-ExecutionPolicy RemoteSigned
- 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.
- 将 的位置添加
conda
到您的 PATH(如果您没有通过安装程序添加它)。对于我在“所有用户”安装这是C:\ProgramData\Anaconda\Scripts
- 从管理员 Powershell 提示更改 Powershell 执行策略为远程签名即
Set-ExecutionPolicy RemoteSigned
- 打开 Anaconda Prompt 并运行
conda init powershell
,这会将与 Conda 相关的启动添加到用户配置文件中某处的 Powershell profile.ps1。
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.
当您运行 VS Code 并调试 .py 或启动 Powershell 集成终端时,您现在应该会看到一条有关“加载个人和系统配置文件”的消息,并且终端提示中带有前缀的 Conda 环境。
回答by PhoenixQ
For me, this articlesolved my issue (on Windows 10): Specifically, the settings.json entry: terminal.integrated.shellArgs.windows
e.g.:
对我来说,这篇文章解决了我的问题(在 Windows 10 上):具体来说,settings.json 条目:terminal.integrated.shellArgs.windows
例如:
"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.
它适用于命令行,但似乎不适用于 PowerShell。
回答by Andre Barbosa
Adding this setting to settings.json solved for me:
将此设置添加到 settings.json 为我解决了:
"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.
PS:此答案基于 kenlukas 的答案,但适用于 powershell 而不是 cmd。
回答by Aung Htet
If you launch VS Code application from inside the Anaconda Navigator application, you automatically get a "conda-aware" VSCode integrated terminal.
如果您从 Anaconda Navigator 应用程序内部启动 VS Code 应用程序,您会自动获得一个“conda-aware”的 VSCode 集成终端。
This way you don't need to change the settings in VS Code itself.
这样你就不需要更改 VS Code 本身的设置。
回答by Benny
Following the trails of @andre-barbosa and @kenlukas, the below configuration works well for me (PowerShell on Windows 10):
按照@andre-barbosa 和@kenlukas 的做法,以下配置对我来说效果很好(Windows 10 上的 PowerShell):
"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.
用您的实际路径替换“ your-path-to-\conda-hook.ps1”和“ your-path-to-the-base-conda-environment”,并将其添加到VSCode的settings.json
. 查看“Anaconda Powershell Prompt”快捷方式的属性,您将找到所需的内容。
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.
关键是要真正遵守 VSCode 的 JSON 配置文件的规则,将整个参数字符串转换为逗号分隔的列表。