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

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


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.


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

CMD parameters & options


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 offREM source bin/activateif "%1" == "bin/activate" (    if not EXIST "%CD%\Scripts\activate.bat" goto notfound    set WRAPEX=Scripts\activate.bat) ELSE (       set WRAPEX=%*)call %WRAPEX%goto :eof:notfoundecho Cannot find the activate script -- aborting.goto :eof