Skip to content

Instantly share code, notes, and snippets.

@MahmadSharaf
Created January 18, 2022 20:57
Show Gist options
  • Select an option

  • Save MahmadSharaf/aafa150e0311e139cef72af2559b1e23 to your computer and use it in GitHub Desktop.

Select an option

Save MahmadSharaf/aafa150e0311e139cef72af2559b1e23 to your computer and use it in GitHub Desktop.
Python Virtual Environment Wrapper for PowerShell
# Adding this script to PS profile allows quick actions for Python venv library.
# Create a variable where virtual environments are located
$VENV_HOME="$HOME\.virtualenvs\"
# A function to list all available virtual environments
function Get-Venvs{
Get-ChildItem -Path $VENV_HOME -Name
}
# A class that returns an array of strings for the available virtual environments.
# It is used for Tab-Completion
Class VenvNames : System.Management.Automation.IValidateSetValuesGenerator {
[string[]] GetValidValues() {
$VenvNames = ForEach ($VenvPath in $global:VENV_HOME) {
If (Test-Path $VenvPath) {
(Get-ChildItem $VenvPath).BaseName
}
}
return [string[]] $VenvNames
}
}
# A function that activates a virtual environment. It supports Tab-Completion
function Activate-Venv{
Param
(
[parameter(Mandatory=$true)]
[ValidateSet([VenvNames])]
[String[]]
$venv
)
& $VENV_HOME$venv\Scripts\activate.ps1
}
# A function that create a new environment
function Create-Venv{
param($venv)
python -m venv $VENV_HOME/$venv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment