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

后端 未结 3 1820
情歌与酒
情歌与酒 2020-12-13 09:05

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 vi

3条回答
  •  再見小時候
    2020-12-13 09:46

    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
    

提交回复
热议问题