@@ -296,48 +296,14 @@ https://github.com/DavidWhittingham/ps-EnablePython
296
296
#>
297
297
298
298
process {
299
- $pythons = New-Object System.Collections.Generic.List[PSObject ]
299
+ $pythons = getPep514Pythons
300
+ $arcGisProPython = getArcGisProPython
300
301
301
- $regKeyLocations = (
302
- @ {
303
- path = " HKCU\Software\Python\"
304
- scope = " CurrentUser"
305
- },
306
- @ {
307
- path = " HKLM\Software\Python\"
308
- scope = " AllUsers"
309
- }
310
- )
311
-
312
- if (is64Bit) {
313
- $regKeyLocations += @ {
314
- path = " HKLM\SOFTWARE\Wow6432Node\Python\"
315
- scope = " AllUsers"
316
- }
317
- }
318
-
319
- foreach ($location in $regKeyLocations ) {
320
- $companyKeys = Get-ChildItem - Path " Registry::$ ( $location.path ) " - ErrorAction " SilentlyContinue"
321
-
322
- foreach ($companyKey in $companyKeys ) {
323
-
324
- if ($companyKey.PSChildName -eq " PyLauncher" ) {
325
- # PyLauncher should be ignored
326
- continue
327
- }
328
-
329
- $tagKeys = Get-ChildItem - Path $companyKey.PSPath - ErrorAction " SilentlyContinue"
330
-
331
- foreach ($tagKey in $tagKeys ) {
332
- if (Test-Path (Join-Path $tagKey.PSPath " \InstallPath" )) {
333
- # tests if a valid install actually exists, uninstalled version can leave the Company/Tag structure
334
- $pythons.Add ((createPythonVersion $tagKey $location.scope ))
335
- }
336
- }
337
- }
302
+ if ($null -ne $arcGisProPython ) {
303
+ $pythons = $pythons + @ ($arcGisProPython )
338
304
}
339
305
340
- # sort python core first
306
+ # Sort the list of Python versions
341
307
$sortProperties = (
342
308
@ {
343
309
Expression = " Scope" ;
@@ -361,6 +327,7 @@ https://github.com/DavidWhittingham/ps-EnablePython
361
327
}
362
328
)
363
329
330
+ # Apply filters
364
331
if (! [string ]::IsNullOrWhiteSpace($Scope )) {
365
332
$pythons = ($pythons | Where-Object { " $ ( $_.Scope ) " -like " $Scope " })
366
333
}
@@ -381,14 +348,15 @@ https://github.com/DavidWhittingham/ps-EnablePython
381
348
$pythons = ($pythons | Where-Object { " $ ( $_.Platform ) " -like " $Platform " })
382
349
}
383
350
351
+ # Sort PythonCore first
384
352
@ (
385
353
@ ($pythons | Where-Object { $_.Company -eq " PythonCore" } | Sort-Object - Property $sortProperties ) +
386
354
@ ($pythons | Where-Object { $_.Company -ne " PythonCore" } | Sort-Object - Property $sortProperties )
387
355
)
388
356
}
389
357
}
390
358
391
- function createPythonVersion ([Microsoft.Win32.RegistryKey ]$tagKey , [string ]$scope ) {
359
+ function createPep514PythonVersion ([Microsoft.Win32.RegistryKey ]$tagKey , [string ]$scope ) {
392
360
$parentKey = Get-Item $tagKey.PSParentPath
393
361
$installPathKey = Get-Item (Join-Path $tagKey.PSPath " InstallPath" )
394
362
$pythonExecutable = getPythonCommand $installPathKey
@@ -429,14 +397,107 @@ function createPythonVersion([Microsoft.Win32.RegistryKey]$tagKey, [string]$scop
429
397
return (Join-Path $this.InstallPath " Scripts" )
430
398
}
431
399
432
- $defaultProperties = @ (" Name" , " Company" , " Tag" , " Version" , " Platform" , " Scope" )
433
- $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(" DefaultDisplayPropertySet" , [string []]$defaultProperties )
434
- $PSStandardMembers = [System.Management.Automation.PSMemberInfo []]@ ($defaultDisplayPropertySet )
435
- $obj | Add-Member MemberSet PSStandardMembers $PSStandardMembers
400
+ $obj = setPythonVersionStandardMembers $obj
436
401
437
402
$obj
438
403
}
439
404
405
+ function getArcGisProPython () {
406
+ $arcgisProRegKey = " Registry::HKEY_LOCAL_MACHINE\SOFTWARE\ESRI\ArcGISPro"
407
+
408
+ if (-not (Test-Path $arcgisProRegKey )) {
409
+ return $null
410
+ }
411
+
412
+ $condaEnv = Get-ItemProperty - Path $arcgisProRegKey - Name " PythonCondaEnv" - ErrorAction " SilentlyContinue"
413
+ $pythonRoot = Get-ItemProperty - Path $arcgisProRegKey - Name " PythonCondaRoot" - ErrorAction " SilentlyContinue"
414
+
415
+ if ((-not $condaEnv ) -or (-not $pythonRoot )) {
416
+ return $null
417
+ }
418
+
419
+ $pythonInstallDir = Join-Path - Path (Join-Path - Path $pythonRoot.PythonCondaRoot - ChildPath " envs" ) - ChildPath $condaEnv.PythonCondaEnv
420
+ $pythonExecutable = Join-Path - path $pythonInstallDir - ChildPath " python.exe"
421
+
422
+ if (-not (Test-Path $pythonExecutable )) {
423
+ return $null
424
+ }
425
+
426
+ # format is "Platform|Tag|Version"
427
+ $pythonInfoCommand = ' import platform; import sys; import sysconfig; print(""{}|{}|{}"".format(sysconfig.get_platform(), sys.winver, platform.python_version()))'
428
+ $pythonInfo = (& $pythonExecutable - E - c $pythonInfoCommand ).Split(" |" )
429
+
430
+ $company = " Esri"
431
+ $companyDisplayName = " Esri Inc."
432
+ $platform = if ($pythonInfo [0 ] -eq " win-amd64" ) { " 64" } elseif ($pythonInfo [0 ] -eq " win32" ) { " 32" } else { $null }
433
+ $tag = $pythonInfo [1 ]
434
+ $tagDisplayName = " Python {0} ({1}-bit)" -f $tag , $platform
435
+ $version = $pythonInfo [2 ]
436
+ $name = " {0} {1}" -f $companyDisplayName , $tagDisplayName
437
+ $scriptsPath = Join-Path $pythonInstallDir " Scripts"
438
+
439
+ $arcgisProPython = New-Object - TypeName PSObject - Prop (@ {
440
+ " Company" = $company ;
441
+ " CompanyDisplayName" = $companyDisplayName ;
442
+ " InstallPath" = $pythonInstallDir ;
443
+ " Executable" = $pythonExecutable ;
444
+ " Tag" = $tag ;
445
+ " TagDisplayName" = $tagDisplayName ;
446
+ " Platform" = $platform ;
447
+ " Version" = $version ;
448
+ " Scope" = " AllUsers" ;
449
+ " Name" = $name ;
450
+ " ScriptsPath" = $scriptsPath ;
451
+ })
452
+
453
+ return setPythonVersionStandardMembers $arcgisProPython
454
+ }
455
+
456
+ function getPep514Pythons () {
457
+ $pythons = New-Object System.Collections.Generic.List[PSObject ]
458
+
459
+ $regKeyLocations = (
460
+ @ {
461
+ path = " HKCU\Software\Python\"
462
+ scope = " CurrentUser"
463
+ },
464
+ @ {
465
+ path = " HKLM\Software\Python\"
466
+ scope = " AllUsers"
467
+ }
468
+ )
469
+
470
+ if (is64Bit) {
471
+ $regKeyLocations += @ {
472
+ path = " HKLM\SOFTWARE\Wow6432Node\Python\"
473
+ scope = " AllUsers"
474
+ }
475
+ }
476
+
477
+ foreach ($location in $regKeyLocations ) {
478
+ $companyKeys = Get-ChildItem - Path " Registry::$ ( $location.path ) " - ErrorAction " SilentlyContinue"
479
+
480
+ foreach ($companyKey in $companyKeys ) {
481
+
482
+ if ($companyKey.PSChildName -eq " PyLauncher" ) {
483
+ # PyLauncher should be ignored
484
+ continue
485
+ }
486
+
487
+ $tagKeys = Get-ChildItem - Path $companyKey.PSPath - ErrorAction " SilentlyContinue"
488
+
489
+ foreach ($tagKey in $tagKeys ) {
490
+ if (Test-Path (Join-Path $tagKey.PSPath " \InstallPath" )) {
491
+ # tests if a valid install actually exists, uninstalled version can leave the Company/Tag structure
492
+ $pythons.Add ((createPep514PythonVersion $tagKey $location.scope ))
493
+ }
494
+ }
495
+ }
496
+ }
497
+
498
+ $pythons
499
+ }
500
+
440
501
function getPythonCommand ([Microsoft.Win32.RegistryKey ]$installPathKey ) {
441
502
# test for "ExecutablePath" folder
442
503
if ((Get-ItemProperty - Path $installPathKey.PSPath ) -match " ExecutablePath" ) {
@@ -454,11 +515,11 @@ function getPythonCommand([Microsoft.Win32.RegistryKey]$installPathKey) {
454
515
}
455
516
456
517
function getPythonVersion ([System.Management.Automation.ApplicationInfo ]$executablePath ) {
457
- if ($executablePath -eq $null ) {
518
+ if ($null -eq $executablePath ) {
458
519
$null
459
520
}
460
521
else {
461
- & $executablePath - E - c ' from sys import version_info ; print(""{0}.{1}.{2}"".format(version_info[0], version_info[1], version_info[2] ))'
522
+ & $executablePath - E - c ' import platform ; print(platform.python_version( ))'
462
523
}
463
524
}
464
525
@@ -467,4 +528,13 @@ function is64Bit {
467
528
((Get-CimInstance Win32_OperatingSystem).OSArchitecture -match ' 64-bit' )
468
529
}
469
530
531
+ function setPythonVersionStandardMembers ($pythonVersion ) {
532
+ $defaultProperties = @ (" Name" , " Company" , " Tag" , " Version" , " Platform" , " Scope" )
533
+ $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(" DefaultDisplayPropertySet" , [string []]$defaultProperties )
534
+ $PSStandardMembers = [System.Management.Automation.PSMemberInfo []]@ ($defaultDisplayPropertySet )
535
+ $pythonVersion | Add-Member MemberSet PSStandardMembers $PSStandardMembers
536
+
537
+ $pythonVersion
538
+ }
539
+
470
540
Export-ModuleMember " *-*"
0 commit comments