activate.ps1 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <#
  2. .Synopsis
  3. Activate a Python virtual environment for the current PowerShell session.
  4. .Description
  5. Pushes the python executable for a virtual environment to the front of the
  6. $Env:PATH environment variable and sets the prompt to signify that you are
  7. in a Python virtual environment. Makes use of the command line switches as
  8. well as the `pyvenv.cfg` file values present in the virtual environment.
  9. .Parameter VenvDir
  10. Path to the directory that contains the virtual environment to activate. The
  11. default value for this is the parent of the directory that the activate.ps1
  12. script is located within.
  13. .Parameter Prompt
  14. The prompt prefix to display when this virtual environment is activated. By
  15. default, this prompt is the name of the virtual environment folder (VenvDir)
  16. surrounded by parentheses and followed by a single space (ie. '(.venv) ').
  17. .Example
  18. activate.ps1
  19. Activates the Python virtual environment that contains the activate.ps1 script.
  20. .Example
  21. activate.ps1 -Verbose
  22. Activates the Python virtual environment that contains the activate.ps1 script,
  23. and shows extra information about the activation as it executes.
  24. .Example
  25. activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
  26. Activates the Python virtual environment located in the specified location.
  27. .Example
  28. activate.ps1 -Prompt "MyPython"
  29. Activates the Python virtual environment that contains the activate.ps1 script,
  30. and prefixes the current prompt with the specified string (surrounded in
  31. parentheses) while the virtual environment is active.
  32. .Notes
  33. On Windows, it may be required to enable this activate.ps1 script by setting the
  34. execution policy for the user. You can do this by issuing the following PowerShell
  35. command:
  36. PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
  37. For more information on Execution Policies:
  38. https://go.microsoft.com/fwlink/?LinkID=135170
  39. #>
  40. Param(
  41. [Parameter(Mandatory = $false)]
  42. [String]
  43. $VenvDir,
  44. [Parameter(Mandatory = $false)]
  45. [String]
  46. $Prompt
  47. )
  48. function Get-PyVenvConfig(
  49. [String]
  50. $ConfigDir
  51. ) {
  52. Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
  53. $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
  54. $pyvenvConfig = @{ }
  55. if ($pyvenvConfigPath) {
  56. Write-Verbose "File exists, parse ``key = value`` lines"
  57. $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
  58. $pyvenvConfigContent | ForEach-Object {
  59. $keyval = $PSItem -split "\s*=\s*", 2
  60. if ($keyval[0] -and $keyval[1]) {
  61. $val = $keyval[1]
  62. if ("'""".Contains($val.Substring(0, 1))) {
  63. $val = $val.Substring(1, $val.Length - 2)
  64. }
  65. $pyvenvConfig[$keyval[0]] = $val
  66. Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
  67. }
  68. }
  69. }
  70. return $pyvenvConfig
  71. }
  72. function global:deactivate([switch] $NonDestructive) {
  73. if (Test-Path variable:_OLD_VIRTUAL_PATH) {
  74. $env:PATH = $variable:_OLD_VIRTUAL_PATH
  75. Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global
  76. }
  77. if (Test-Path variable:_OLD_VIRTUAL_TCL_LIBRARY) {
  78. $env:TCL_LIBRARY = $variable:_OLD_VIRTUAL_TCL_LIBRARY
  79. Remove-Variable "_OLD_VIRTUAL_TCL_LIBRARY" -Scope global
  80. } else {
  81. if (Test-Path env:TCL_LIBRARY) {
  82. Remove-Item env:TCL_LIBRARY -ErrorAction SilentlyContinue
  83. }
  84. }
  85. if (Test-Path variable:_OLD_VIRTUAL_TK_LIBRARY) {
  86. $env:TK_LIBRARY = $variable:_OLD_VIRTUAL_TK_LIBRARY
  87. Remove-Variable "_OLD_VIRTUAL_TK_LIBRARY" -Scope global
  88. } else {
  89. if (Test-Path env:TK_LIBRARY) {
  90. Remove-Item env:TK_LIBRARY -ErrorAction SilentlyContinue
  91. }
  92. }
  93. if (Test-Path variable:_OLD_PKG_CONFIG_PATH) {
  94. $env:PKG_CONFIG_PATH = $variable:_OLD_PKG_CONFIG_PATH
  95. Remove-Variable "_OLD_PKG_CONFIG_PATH" -Scope global
  96. }
  97. if (Test-Path function:_old_virtual_prompt) {
  98. $function:prompt = $function:_old_virtual_prompt
  99. Remove-Item function:\_old_virtual_prompt
  100. }
  101. if ($env:VIRTUAL_ENV) {
  102. Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue
  103. }
  104. if ($env:VIRTUAL_ENV_PROMPT) {
  105. Remove-Item env:VIRTUAL_ENV_PROMPT -ErrorAction SilentlyContinue
  106. }
  107. if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
  108. Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
  109. }
  110. if (!$NonDestructive) {
  111. Remove-Item function:deactivate
  112. Remove-Item function:pydoc
  113. }
  114. }
  115. function global:pydoc {
  116. & python -m pydoc $args
  117. }
  118. $VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
  119. $VenvExecDir = Get-Item -Path $VenvExecPath
  120. Write-Verbose "Activation script is located in path: '$VenvExecPath'"
  121. if ($VenvDir) {
  122. Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
  123. } else {
  124. $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
  125. Write-Verbose "VenvDir=$VenvDir"
  126. }
  127. $pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
  128. if ($Prompt) {
  129. Write-Verbose "Prompt specified as argument, using '$Prompt'"
  130. } else {
  131. if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
  132. Write-Verbose "Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
  133. $Prompt = $pyvenvCfg['prompt']
  134. } elseif (__VIRTUAL_PROMPT__ -ne "") {
  135. $Prompt = __VIRTUAL_PROMPT__
  136. } else {
  137. $Prompt = Split-Path -Path $VenvDir -Leaf
  138. }
  139. }
  140. deactivate -nondestructive
  141. $env:VIRTUAL_ENV = $VenvDir
  142. $env:VIRTUAL_ENV_PROMPT = $Prompt
  143. if (__TCL_LIBRARY__ -ne "") {
  144. if (Test-Path env:TCL_LIBRARY) {
  145. New-Variable -Scope global -Name _OLD_VIRTUAL_TCL_LIBRARY -Value $env:TCL_LIBRARY
  146. }
  147. $env:TCL_LIBRARY = __TCL_LIBRARY__
  148. }
  149. if (__TK_LIBRARY__ -ne "") {
  150. if (Test-Path env:TK_LIBRARY) {
  151. New-Variable -Scope global -Name _OLD_VIRTUAL_TK_LIBRARY -Value $env:TK_LIBRARY
  152. }
  153. $env:TK_LIBRARY = __TK_LIBRARY__
  154. }
  155. New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH
  156. if (Test-Path env:PKG_CONFIG_PATH) {
  157. New-Variable -Scope global -Name _OLD_PKG_CONFIG_PATH -Value $env:PKG_CONFIG_PATH
  158. }
  159. $env:PKG_CONFIG_PATH = "$env:VIRTUAL_ENV\lib\pkgconfig;$env:PKG_CONFIG_PATH"
  160. $env:PATH = "$env:VIRTUAL_ENV/" + __BIN_NAME__ + __PATH_SEP__ + $env:PATH
  161. if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
  162. function global:_old_virtual_prompt {
  163. ""
  164. }
  165. $function:_old_virtual_prompt = $function:prompt
  166. New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
  167. function global:prompt {
  168. Write-Host -NoNewline -ForegroundColor Green "($($_PYTHON_VENV_PROMPT_PREFIX)) "
  169. _old_virtual_prompt
  170. }
  171. }