Functions/Enter-PythonVirtualEnvironment.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
. "$PSScriptRoot\ConfigGettersAndSetters.ps1"

function Enter-PythonVirtualEnvironment([string]$Name){
$Null = @(
    $virtualenvRoot = Get-VirtualEnvironmentRoot

    #Find matching virtualenv
    $virtualEnvs = Get-ChildItem $virtualenvRoot -Directory
    foreach ($environmentPath in $virtualEnvs){
        $environmentName = $environmentPath.Name
        if ($environmentName.StartsWith($Name)){
            Write-Host "To exit the virtual environment execute the command, 'deactivate'."
            $activationPath = "$virtualenvRoot\$environmentName\Scripts\Activate.ps1" -replace ' ','` '
            Invoke-Expression -Command $activationPath
            return
        }
    }

    throw "Could not find a virtual environment in $virtualenvRoot whose name starts with $Name."
)
}