Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Activate.ps1 8.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  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. ttps:/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 declarations --------------------------------------------------- #>
  49. <#
  50. .Synopsis
  51. Remove all shell session elements added by the Activate script, including the
  52. addition of the virtual environment's Python executable from the beginning of
  53. the PATH variable.
  54. .Parameter NonDestructive
  55. If present, do not remove this function from the global namespace for the
  56. session.
  57. #>
  58. function global:deactivate ([switch]$NonDestructive) {
  59. # Revert to original values
  60. # The prior prompt:
  61. if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
  62. Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
  63. Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
  64. }
  65. # The prior PYTHONHOME:
  66. if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
  67. Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
  68. Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
  69. }
  70. # The prior PATH:
  71. if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
  72. Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
  73. Remove-Item -Path Env:_OLD_VIRTUAL_PATH
  74. }
  75. # Just remove the VIRTUAL_ENV altogether:
  76. if (Test-Path -Path Env:VIRTUAL_ENV) {
  77. Remove-Item -Path env:VIRTUAL_ENV
  78. }
  79. # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
  80. if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
  81. Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
  82. }
  83. # Leave deactivate function in the global namespace if requested:
  84. if (-not $NonDestructive) {
  85. Remove-Item -Path function:deactivate
  86. }
  87. }
  88. <#
  89. .Description
  90. Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
  91. given folder, and returns them in a map.
  92. For each line in the pyvenv.cfg file, if that line can be parsed into exactly
  93. two strings separated by `=` (with any amount of whitespace surrounding the =)
  94. then it is considered a `key = value` line. The left hand string is the key,
  95. the right hand is the value.
  96. If the value starts with a `'` or a `"` then the first and last character is
  97. stripped from the value before being captured.
  98. .Parameter ConfigDir
  99. Path to the directory that contains the `pyvenv.cfg` file.
  100. #>
  101. function Get-PyVenvConfig(
  102. [String]
  103. $ConfigDir
  104. ) {
  105. Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
  106. # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
  107. $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
  108. # An empty map will be returned if no config file is found.
  109. $pyvenvConfig = @{ }
  110. if ($pyvenvConfigPath) {
  111. Write-Verbose "File exists, parse `key = value` lines"
  112. $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
  113. $pyvenvConfigContent | ForEach-Object {
  114. $keyval = $PSItem -split "\s*=\s*", 2
  115. if ($keyval[0] -and $keyval[1]) {
  116. $val = $keyval[1]
  117. # Remove extraneous quotations around a string value.
  118. if ("'""".Contains($val.Substring(0, 1))) {
  119. $val = $val.Substring(1, $val.Length - 2)
  120. }
  121. $pyvenvConfig[$keyval[0]] = $val
  122. Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
  123. }
  124. }
  125. }
  126. return $pyvenvConfig
  127. }
  128. <# Begin Activate script --------------------------------------------------- #>
  129. # Determine the containing directory of this script
  130. $VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
  131. $VenvExecDir = Get-Item -Path $VenvExecPath
  132. Write-Verbose "Activation script is located in path: '$VenvExecPath'"
  133. Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
  134. Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
  135. # Set values required in priority: CmdLine, ConfigFile, Default
  136. # First, get the location of the virtual environment, it might not be
  137. # VenvExecDir if specified on the command line.
  138. if ($VenvDir) {
  139. Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
  140. }
  141. else {
  142. Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
  143. $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
  144. Write-Verbose "VenvDir=$VenvDir"
  145. }
  146. # Next, read the `pyvenv.cfg` file to determine any required value such
  147. # as `prompt`.
  148. $pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
  149. # Next, set the prompt from the command line, or the config file, or
  150. # just use the name of the virtual environment folder.
  151. if ($Prompt) {
  152. Write-Verbose "Prompt specified as argument, using '$Prompt'"
  153. }
  154. else {
  155. Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
  156. if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
  157. Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
  158. $Prompt = $pyvenvCfg['prompt'];
  159. }
  160. else {
  161. Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virutal environment)"
  162. Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
  163. $Prompt = Split-Path -Path $venvDir -Leaf
  164. }
  165. }
  166. Write-Verbose "Prompt = '$Prompt'"
  167. Write-Verbose "VenvDir='$VenvDir'"
  168. # Deactivate any currently active virtual environment, but leave the
  169. # deactivate function in place.
  170. deactivate -nondestructive
  171. # Now set the environment variable VIRTUAL_ENV, used by many tools to determine
  172. # that there is an activated venv.
  173. $env:VIRTUAL_ENV = $VenvDir
  174. if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
  175. Write-Verbose "Setting prompt to '$Prompt'"
  176. # Set the prompt to include the env name
  177. # Make sure _OLD_VIRTUAL_PROMPT is global
  178. function global:_OLD_VIRTUAL_PROMPT { "" }
  179. Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT
  180. New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
  181. function global:prompt {
  182. Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
  183. _OLD_VIRTUAL_PROMPT
  184. }
  185. }
  186. # Clear PYTHONHOME
  187. if (Test-Path -Path Env:PYTHONHOME) {
  188. Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
  189. Remove-Item -Path Env:PYTHONHOME
  190. }
  191. # Add the venv to the PATH
  192. Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
  193. $Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...