20

This is enough to reproduce the issue:

Save as test.bat

:: Create Conda env
set name=%1
conda create -n %name% python -y
activate %name%
echo "Never gets here"
:: script should continue below...

Run from cmd.

>test.bat "testname"

Output:

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>

And that's it. The echo statement doesn't execute, but there is no error message.

Why does activating the conda env halt the batch script, and is there a way around it?

Jamie Bull
  • 12,889
  • 15
  • 77
  • 116
  • Possible duplicate of [Activate virtual environement and start jupyter notebook all in batch file](http://stackoverflow.com/questions/42353108/activate-virtual-environement-and-start-jupyter-notebook-all-in-batch-file) – darthbith Mar 10 '17 at 01:33

3 Answers3

38

use

call activate %name%
  • I'm assuming that activate is a batch file. If you call it, processing will return after that batch is finished. Without the call, execution is transferred to activate and ends when activate ends.
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    I need to be in the environment to continue – Jamie Bull Mar 09 '17 at 11:30
  • 4
    @JamieBull With respect to Conda, `being in an environment` is basically having ~4 environment variables set. If you use `CALL`, the environment variables set by the activate batch file will be retained on return. See: https://paste.fedoraproject.org/paste/Rd5AW3qrqxXkTCt6Ma7Qx15M1UNdIGYhyRLivL9gydE=/raw – Nehal J Wani Mar 10 '17 at 10:55
0

As discussed in the comment by asker @Jamie Bull himself.

I need to be in the environment to continue

In my case, to describe it more precisely, how could I activate a CONDA ENV, change to a working directory directly in one-click, or in one-command?

From a Linux background, we are more likely to complete this request by a simple one-line BASH script. I was facing the same problem with BAT file, as discussed here, CMD batch file's behavior is not that suitable for this task. Using CALL directive within BAT does not help either.

Luckily, CONDA now is packaged with PowerShell's PS1 start script, and the according shortcut left me with another choice, after a few test, it works.

My final solution is to create a Windows shortcut for my purpose, i.e, open PyTorch ENV or Tensorflow ENV in one click. I just made a copy of CONDA's package shortcut, did the edit on the copy itself, than the edited shortcut is ready to use. The screenshot will explain it well.

windows shortcut for CONDA ENV activate

For PS1 script, it is finally as easy as BASH now:

# tf_env.ps1: Activate ENV and go to working directory
conda activate tf-gpu
cd C:\Tensorflow.Playground
Jerry Tian
  • 3,439
  • 4
  • 27
  • 29
-2

try adding a new line at the end of your .bat file

avloss
  • 2,389
  • 2
  • 22
  • 26
  • That´s not actually the end, just in the example. I'll edit to make that clear – Jamie Bull Mar 09 '17 at 10:51
  • ah, you're right. to think of it - it can't possibly continue executing - since you've started a new shell session. It's same with ssh. Imagine you do `run-locally.sh` `ssh remote@server` `run-on-the-remote-server.sh` remote command would never execute, because it will give you back control once it's logged in – avloss Mar 09 '17 at 10:54
  • you can't use `activate` (as I imagine), but you can call any python binary (you can use binary from virtual env directly) - no need to run "activate" – avloss Mar 09 '17 at 10:56
  • Ah, yes. I don't actually need to activate the env until the very end of my script. Thanks, I think it's sorted now – Jamie Bull Mar 09 '17 at 10:57
  • 1
    but if there was a solution - it would look something like this. `conda activate EVN_NAME --execute second_part_of_the_script.bat` – avloss Mar 09 '17 at 10:57
  • yeah - I don't think it's possible, sorry for giving you a wrong idea. What I meant to say by that "what if", is that whatever happens after `activate`, should be somehow passed as a script at that stage. But that's probably impossible. – avloss Mar 09 '17 at 13:57
  • I've gone with making two scripts. the first leaves me in the env, then the second does the rest, and still reduces my typing considerably! – Jamie Bull Mar 09 '17 at 14:00