42

I've done a fair bit of bash scripting, but very little batch scripting on Windows. I'm trying to activate a Python virtualenv, run a Python script, then deactivate the virtualenv when the script exits.

I've got a folder called env, which is my virtualenv, and a folder called work, which contains my scripts.

This is what I've got so far:

%~dp0env\Scripts\activate.bat
python %~dp0work\script.py
deactivate

However, when I run the script, it activates the virtualenv then stops. It does not get to the second line and run the Python script. Is there a way to "source" the activate script folder, so that the rest of the batch script can be run as if I'd called activate.bat from the command line?

3 Answers 3

65

I'd say you just need to prepend 'call' to your activate.bat invocation, to ensure that the current batch file is resumed after activate is executed:

call %~dp0env\Scripts\activate.bat

Consider doing the same for deactivate.bat. Furthermore, if you want to ensure that the current cmd.exe environment is not polluted by a call to your batch file, consider wrapping your commands in a setlocal/endlocal command pair.

6
  • I'll try this out, but this seems like exactly what I want. Thanks! Commented Jul 21, 2011 at 20:59
  • @Nicola If batch1.bat contains batch2.bat and echo 1, and batch2.bat contains echo 2, and I run batch1.bat, I see a new shell open, then 2, then 1, so I don't think this is the problem.
    – agf
    Commented Jul 21, 2011 at 21:07
  • From the "Call" command documentation: "Calls one batch program from another without stopping the parent batch program." (technet.microsoft.com/en-us/library/cc732835%28WS.10%29.aspx) Commented Jul 21, 2011 at 21:16
  • @Nicola Calls one batch program from another without stopping the parent batch program. That's not what you want here, right? :) Commented Aug 18, 2011 at 8:23
  • 2
    It's exactly what we want. The docs aren't very clear, they should say terminating rather than stopping. What they mean isn't some sort of parallel execution; rather, if you don't use the call command the execution of the calling batch is replaced with the execution of the called one, i.e. subsequent instructions in the calling batch are discarded. With the call command execution of the calling batch resumes after the called one terminates. Commented Aug 18, 2011 at 9:10
6

I made a .lnk file that points to cmd /k "path/to the/script/activate.bat", and it works.

CMD parameters & options

2

I suppose you just want to perform the same commands in Windows as if expected in Linux Bash/shell. When I want to start a virtualenv I am actually in its top directory, and the Linux command would be "source bin/activate".

It is no problem to simulate this behaviour on Windows. Me personally, I've put a batch file named activate.bat somewhere on the PATH environment variable like this:

:: activate.bat
@echo off
REM source bin/activate
if "%1" == "bin/activate" (
    if not EXIST "%CD%\Scripts\activate.bat" goto notfound
    set WRAPEX=Scripts\activate.bat
) ELSE (
       set WRAPEX=%*
)
call %WRAPEX%
goto :eof

:notfound
echo Cannot find the activate script -- aborting.
goto :eof

Not the answer you're looking for? Browse other questions tagged or ask your own question.