Batch equivalent of "source" on Windows: how to run a Python script from a virtualenv

28,957

Solution 1

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.

Solution 2

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

CMD parameters & options

Solution 3

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
Share:
28,957
jmite
Author by

jmite

I am Joey Eremondi, a PhD Student at the University of British Columbia. I do research in Programming Languages and Theory of Computation, particularly with dependent types. My Masters Thesis was on improving error messages for higher order unification. I've also co-authored a few papers on reversal-bounded counter automata. I have an M.Sc in Computing Science from Utrecht University, a B.Sc. Honours in Computer Science, and a B.Sc. 4-year in Mathematics, both from the University of Saskatchewan.

Updated on March 28, 2020

Comments

  • jmite
    jmite about 4 years

    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?

  • jmite
    jmite almost 13 years
    I'll try this out, but this seems like exactly what I want. Thanks!
  • agf
    agf almost 13 years
    @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.
  • Nicola Musatti
    Nicola Musatti almost 13 years
    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.asp‌​x)
  • Piotr Dobrogost
    Piotr Dobrogost over 12 years
    @Nicola Calls one batch program from another without stopping the parent batch program. That's not what you want here, right? :)
  • Nicola Musatti
    Nicola Musatti over 12 years
    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.
  • danodonovan
    danodonovan about 11 years
    @jmite if this did solve your problem, it's nice to 'accept' it :)
  • Majid
    Majid almost 11 years
    Thank you, that is exactly what I was looking for.