如何在 VSCode 中激活 conda 环境 Windows 10
How to activate conda environment in VSCode on Windows 10
每当我在 Windows 10 上尝试在 VSCode 内激活 conda 时,我都会收到一条初始化 shell.
的消息
我尝试了以下方法
conda init bash
并重新启动 VSCode
conda.bat activate
-> 无变化。 which python
仍然指向 /c/Users/user/anaconda3/python
conda init bash
后重新启动计算机
- 通过 F1 设置解释器 > Python:Select 解释器
我认为问题出在 VSCode 内,因为我可以从 cmd、powershell、gitbash 和 PyCharm 终端激活 conda
。
我错过了什么吗?
$ conda activate data_science
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- cmd.exe
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
conda 信息
active environment : None
user config file : C:\Users\user\.condarc
populated config files :
conda version : 4.8.3
conda-build version : 3.18.11
python version : 3.7.6.final.0
virtual packages : __cuda=11.0
base environment : C:\Users\user\anaconda3 (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : C:\Users\user\anaconda3\pkgs
C:\Users\user\.conda\pkgs
C:\Users\user\AppData\Local\conda\conda\pkgs
envs directories : C:\Users\user\anaconda3\envs
C:\Users\user\.conda\envs
C:\Users\user\AppData\Local\conda\conda\envs
platform : win-64
user-agent : conda/4.8.3 requests/2.22.0 CPython/3.7.6 Windows/10 Windows/10.0.18362
administrator : False
netrc file : None
offline mode : False
你的“$”向我表明你实际上并没有打开命令行提示,而是打开了一个替代方法(可能 bash?)。
按 Ctrl+Shift+P 并搜索 'Select default shell',更改为命令行提示,您应该可以开始了:)
我遇到了类似的问题,但找不到我需要的明确解决方案。我正在提供我发现对我有用的鹅卵石解决方案,如果上述答案对您不起作用,希望它可能对您有用。
一种方法是进入注册表并更改 ExecutionPolicy(我对这样做没有信心)。
对我有用的方法是在 VSCode 中按 Ctrl+Shift+P 并打开“首选项:打开设置 (Json)”并添加配置文件。
这修复了“CommandNotFoundError:您的 shell 未正确配置为使用 'conda activate'...”错误。
注意:此处的设置假设您使用的功率shell 作为默认值shell。这也修复了一些“& ...”错误。出现错误是因为我相信“&”是一个 PS (powershell) 命令,如果您使用 cmd.exe 作为 shell,则可能会弹出错误。
{...,
"python.pythonPath": "C:\Users\<user>\miniconda3\envs\py39\python.exe",
"python.condaPath": "C:\Users\<user>\miniconda3\Scripts\conda.exe",
"terminal.integrated.cwd": "D:\my_VSCode_storage\py39_env\my_project_folder",
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"python.terminal.executeInFileDir": true,
"args":["-NoLogo",
"-ExecutionPolicy",
"Bypass",
"-NoExit",
"-Command",
"& C:\Users\<user>\miniconda3\shell\condabin\conda-hook.ps1",
";conda activate 'C:\Users\<user>\miniconda3'"
]
},
...}
试试这个命令 source activate base
,应该可以。然后可以输入命令conda activate <env_name>
每 ,执行 conda init
。随后的运行将正确执行对 conda
.
所需的调用
您可以创建一个批处理文件,然后一键打开您激活的环境。
我在这里做了一个例子
https://github.com/Zrt747/condaENVbatch.git
每当我在 Windows 10 上尝试在 VSCode 内激活 conda 时,我都会收到一条初始化 shell.
的消息我尝试了以下方法
conda init bash
并重新启动 VSCodeconda.bat activate
-> 无变化。which python
仍然指向/c/Users/user/anaconda3/python
conda init bash
后重新启动计算机
- 通过 F1 设置解释器 > Python:Select 解释器
我认为问题出在 VSCode 内,因为我可以从 cmd、powershell、gitbash 和 PyCharm 终端激活 conda
。
我错过了什么吗?
$ conda activate data_science
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- cmd.exe
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
conda 信息
active environment : None
user config file : C:\Users\user\.condarc
populated config files :
conda version : 4.8.3
conda-build version : 3.18.11
python version : 3.7.6.final.0
virtual packages : __cuda=11.0
base environment : C:\Users\user\anaconda3 (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : C:\Users\user\anaconda3\pkgs
C:\Users\user\.conda\pkgs
C:\Users\user\AppData\Local\conda\conda\pkgs
envs directories : C:\Users\user\anaconda3\envs
C:\Users\user\.conda\envs
C:\Users\user\AppData\Local\conda\conda\envs
platform : win-64
user-agent : conda/4.8.3 requests/2.22.0 CPython/3.7.6 Windows/10 Windows/10.0.18362
administrator : False
netrc file : None
offline mode : False
你的“$”向我表明你实际上并没有打开命令行提示,而是打开了一个替代方法(可能 bash?)。
按 Ctrl+Shift+P 并搜索 'Select default shell',更改为命令行提示,您应该可以开始了:)
我遇到了类似的问题,但找不到我需要的明确解决方案。我正在提供我发现对我有用的鹅卵石解决方案,如果上述答案对您不起作用,希望它可能对您有用。
一种方法是进入注册表并更改 ExecutionPolicy(我对这样做没有信心)。
对我有用的方法是在 VSCode 中按 Ctrl+Shift+P 并打开“首选项:打开设置 (Json)”并添加配置文件。
这修复了“CommandNotFoundError:您的 shell 未正确配置为使用 'conda activate'...”错误。
注意:此处的设置假设您使用的功率shell 作为默认值shell。这也修复了一些“& ...”错误。出现错误是因为我相信“&”是一个 PS (powershell) 命令,如果您使用 cmd.exe 作为 shell,则可能会弹出错误。
{...,
"python.pythonPath": "C:\Users\<user>\miniconda3\envs\py39\python.exe",
"python.condaPath": "C:\Users\<user>\miniconda3\Scripts\conda.exe",
"terminal.integrated.cwd": "D:\my_VSCode_storage\py39_env\my_project_folder",
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"python.terminal.executeInFileDir": true,
"args":["-NoLogo",
"-ExecutionPolicy",
"Bypass",
"-NoExit",
"-Command",
"& C:\Users\<user>\miniconda3\shell\condabin\conda-hook.ps1",
";conda activate 'C:\Users\<user>\miniconda3'"
]
},
...}
试试这个命令 source activate base
,应该可以。然后可以输入命令conda activate <env_name>
每 conda init
。随后的运行将正确执行对 conda
.
您可以创建一个批处理文件,然后一键打开您激活的环境。 我在这里做了一个例子 https://github.com/Zrt747/condaENVbatch.git