为什么我的批处理脚本在激活新的 conda env 后停止?
Why does my batch script stop after activating a new conda env?
这足以重现问题:
另存为test.bat
:: Create Conda env
set name=%1
conda create -n %name% python -y
activate %name%
echo "Never gets here"
:: script should continue below...
运行 从 cmd.
>test.bat "testname"
输出:
C:\Users\Jamie\git>test.bat testname
C:\Users\Jamie\git>set name=testname
C:\Users\Jamie\git>conda create -n testname python -y
Fetching package metadata ...........
Solving package specifications: .
Package plan for installation in environment C:\Users\Jamie\Miniconda2\envs\testname:
The following NEW packages will be INSTALLED:
pip: 9.0.1-py27_1
python: 2.7.13-0
setuptools: 27.2.0-py27_1
vs2008_runtime: 9.00.30729.5054-0
wheel: 0.29.0-py27_0
#
# To activate this environment, use:
# > activate testname
#
# To deactivate this environment, use:
# > deactivate testname
#
# * for power-users using bash, you must source
#
C:\Users\Jamie\git>activate testname
(testname) C:\Users\Jamie\git>
就是这样。 echo
语句不执行,但没有错误消息。
为什么激活 conda env 会停止批处理脚本,有解决办法吗?
尝试在 .bat
文件末尾添加一个新行
使用
call activate %name%
- 我假设
activate
是一个批处理文件。如果您 call
它,处理将在该批次完成后 return。如果没有 call
,执行将转移到 activate
,并在 activate
结束时结束。
正如提问者@Jamie Bull 本人在评论中所讨论的那样。
I need to be in the environment to continue
在我的例子中,更准确地说,我如何激活 CONDA ENV,直接一键切换到工作目录,或者在一个命令中?
从Linux的背景来看,我们更有可能通过简单的一行BASH脚本来完成这个请求。我在 BAT 文件中遇到了同样的问题,正如这里所讨论的,CMD 批处理文件的行为不适合这个任务。在 BAT 中使用 CALL 指令也无济于事。
幸运的是,CONDA 现在打包了 PowerShell 的 PS1 启动脚本,相应的快捷方式让我有了另一个选择,经过几次测试,它可以工作。
我的最终解决方案是为我的目的创建一个 Windows 快捷方式,即单击打开 PyTorch ENV 或 Tensorflow ENV。我刚刚复制了 CONDA 的包快捷方式,对副本本身进行了编辑,然后编辑的快捷方式就可以使用了。截图会很好的说明。
对于PS1脚本,现在终于和BASH一样简单了:
# tf_env.ps1: Activate ENV and go to working directory
conda activate tf-gpu
cd C:\Tensorflow.Playground
这足以重现问题:
另存为test.bat
:: Create Conda env
set name=%1
conda create -n %name% python -y
activate %name%
echo "Never gets here"
:: script should continue below...
运行 从 cmd.
>test.bat "testname"
输出:
C:\Users\Jamie\git>test.bat testname
C:\Users\Jamie\git>set name=testname
C:\Users\Jamie\git>conda create -n testname python -y
Fetching package metadata ...........
Solving package specifications: .
Package plan for installation in environment C:\Users\Jamie\Miniconda2\envs\testname:
The following NEW packages will be INSTALLED:
pip: 9.0.1-py27_1
python: 2.7.13-0
setuptools: 27.2.0-py27_1
vs2008_runtime: 9.00.30729.5054-0
wheel: 0.29.0-py27_0
#
# To activate this environment, use:
# > activate testname
#
# To deactivate this environment, use:
# > deactivate testname
#
# * for power-users using bash, you must source
#
C:\Users\Jamie\git>activate testname
(testname) C:\Users\Jamie\git>
就是这样。 echo
语句不执行,但没有错误消息。
为什么激活 conda env 会停止批处理脚本,有解决办法吗?
尝试在 .bat
文件末尾添加一个新行
使用
call activate %name%
- 我假设
activate
是一个批处理文件。如果您call
它,处理将在该批次完成后 return。如果没有call
,执行将转移到activate
,并在activate
结束时结束。
正如提问者@Jamie Bull 本人在评论中所讨论的那样。
I need to be in the environment to continue
在我的例子中,更准确地说,我如何激活 CONDA ENV,直接一键切换到工作目录,或者在一个命令中?
从Linux的背景来看,我们更有可能通过简单的一行BASH脚本来完成这个请求。我在 BAT 文件中遇到了同样的问题,正如这里所讨论的,CMD 批处理文件的行为不适合这个任务。在 BAT 中使用 CALL 指令也无济于事。
幸运的是,CONDA 现在打包了 PowerShell 的 PS1 启动脚本,相应的快捷方式让我有了另一个选择,经过几次测试,它可以工作。
我的最终解决方案是为我的目的创建一个 Windows 快捷方式,即单击打开 PyTorch ENV 或 Tensorflow ENV。我刚刚复制了 CONDA 的包快捷方式,对副本本身进行了编辑,然后编辑的快捷方式就可以使用了。截图会很好的说明。
对于PS1脚本,现在终于和BASH一样简单了:
# tf_env.ps1: Activate ENV and go to working directory
conda activate tf-gpu
cd C:\Tensorflow.Playground