在 Windows 10 的终端上一次性运行 Anaconda 的 ipython

在 Windows 10 的终端上一次性运行 Anaconda 的 ipython

正如标题所示,我想一次性在 Windows 10 上运行Anacondaipython最好Windows Terminal (Preview)cmd.exe选项卡中)。

经过一气呵成我的意思是,通过一个快捷方式或批处理文件,我就可以开始使用ipythonWindows 10 中的环境Windows Terminal (perview)

目前我可以通过 3 个步骤完成:

  1. 打开Windows Terminal (Preview)。或者,我可以创建该程序的快捷方式,其路径为C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_0.5.2661.0_x64__8wekyb3d8bbwe\WindowsTerminal.exe。我已更改首选项,以便默认选项卡为cmd.exe而不是PowerShell

  2. 在 中Windows Terminal (Preview),通过输入 来启动 Anaconda 环境C:\Anaconda3\Scripts\activate.bat C:\Anaconda3,其中C:\Anaconda3是我的安装文件夹。然后提示符的每一行都将以 开头(base)

  3. 在提示符中输入ipython。然后 Ipython 将启动。提示符现在变成这样In [1]:

但我不知道如何将上述 3 个步骤合并为一个步骤,使用批处理文件或其他任何方法。任何有助于实现自动化的想法都非常感谢!

编辑:供您参考,内容C:\Anaconda3\Scripts\activate.bat如下

@REM Copyright (C) 2012 Anaconda, Inc
@REM SPDX-License-Identifier: BSD-3-Clause
@REM Test first character and last character of %1 to see if first character is a "
@REM   but the last character isn't.
@REM This was a bug as described in https://github.com/ContinuumIO/menuinst/issues/60
@REM When Anaconda Prompt has the form
@REM   %windir%\system32\cmd.exe "/K" "C:\Users\builder\Miniconda3\Scripts\activate.bat" "C:\Users\builder\Miniconda3"
@REM Rather than the correct
@REM    %windir%\system32\cmd.exe /K ""C:\Users\builder\Miniconda3\Scripts\activate.bat" "C:\Users\builder\Miniconda3""
@REM this solution taken from https://stackoverflow.com/a/31359867
@set "_args1=%1"
@set _args1_first=%_args1:~0,1%
@set _args1_last=%_args1:~-1%
@set _args1_first=%_args1_first:"=+%
@set _args1_last=%_args1_last:"=+%
@set _args1=

@if "%_args1_first%"=="+" if NOT "%_args1_last%"=="+" (
    @CALL "%~dp0..\condabin\conda.bat" activate
    @GOTO :End
)

@REM This may work if there are spaces in anything in %*
@CALL "%~dp0..\condabin\conda.bat" activate %*

:End
@set _args1_first=
@set _args1_last=

答案1

程序:

  • 打开 Windows 终端。按Ctrl+在默认文本编辑器中,打开 文件。settings.json

  • 将以下 JSON 对象添加到profile数组。

{
    "guid": "{ee4fe116-1375-4c00-925c-1e361f99496d}",
    "name": "Anaconda ipython",
    "commandline": "cmd.exe /C C:\\Anaconda3\\Scripts\\activate.bat C:\\Anaconda3 & ipython",
    "hidden": false
},
  • 将值更改defaultProfile为该 GUID 以在启动时自动打开 Anaconda ipython。
"defaultProfile": "{ee4fe116-1375-4c00-925c-1e361f99496d}",

解释:

做什么commandline?它cmd.exeactivate.bat 文件一起执行。/C选项执行命令cmd.exe然后终止。如果要防止终止,请使用/K选项。与号 (&) 表示ipython在执行activate.bat文件后执行命令。

GUID 是使用 随机生成的uuidgen,请检查它是否与该 JSON 文件中的其他配置文件 GUID 不匹配。请参阅这个答案有关如何编辑 JSON 文件的更多详细信息。

相关内容