Use activate.ps1 script instead of activate.bat with virtualenvwrapper

1.7k views Asked by At

I'm trying to setup my developer environment in Windows (I know.. I know..). Cmder uses PowerShell and makes this quite a bit more comfortable. Virtualenv however still is a bit cumbersome, virtualenvwrapper supposedly makes it a bit more easier.

However, virtualenvwrapper is using the activate.bat in its workon.bat command, which has some problems with PowerShell. Since virtualenv supports Powershell out of the box since 1.7.1 (2012-02-16) and offers its own activate.ps1 script that works fine, I would like to use that instead.

The relevant part in workon.bat:

if not exist "%WORKON_HOME%\%VENV%\Scripts\activate.bat" (
    echo.
    echo.    %WORKON_HOME%\%VENV%
    echo.    doesn't contain a virtualenv ^(yet^).
    echo.    Create it with "mkvirtualenv %VENV%"
    goto END
)

call "%WORKON_HOME%\%VENV%\Scripts\activate.bat"
if defined WORKON_OLDTITLE (
    title %1 ^(VirtualEnv^)
)

Simply replacing activate.bat with activate.ps1 doesn't work (opens a new shell or my editor depending on the set default program) and I've never did anything in batch, which is why I'm stuck.

1

There are 1 answers

0
Ansgar Wiechers On BEST ANSWER

Basically, all you need is

& "$env:WORKON_HOME\$env:VENV\Scripts\activate.ps1"

assuming that activate.ps1 is in the same folder as activate.bat. The other batch commands are just for displaying some help text if activate.bat doesn't exist, and for changing the window title. If you want the latter in PowerShell too, you can do it like this:

if ($env:WORKON_OLDTITLE) {
  $host.ui.RawUI.WindowTitle = $args[0] + ' (VirtualEnv)'
}