Skip to content

Commit 295939e

Browse files
Added ability to get "AllUsers" ArcGIS Pro Python
1 parent b6a7bf3 commit 295939e

File tree

1 file changed

+116
-46
lines changed

1 file changed

+116
-46
lines changed

Diff for: EnablePython/EnablePython.psm1

+116-46
Original file line numberDiff line numberDiff line change
@@ -296,48 +296,14 @@ https://github.com/DavidWhittingham/ps-EnablePython
296296
#>
297297

298298
process {
299-
$pythons = New-Object System.Collections.Generic.List[PSObject]
299+
$pythons = getPep514Pythons
300+
$arcGisProPython = getArcGisProPython
300301

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)
338304
}
339305

340-
# sort python core first
306+
# Sort the list of Python versions
341307
$sortProperties = (
342308
@{
343309
Expression = " Scope";
@@ -361,6 +327,7 @@ https://github.com/DavidWhittingham/ps-EnablePython
361327
}
362328
)
363329

330+
# Apply filters
364331
if (![string]::IsNullOrWhiteSpace($Scope)) {
365332
$pythons = ($pythons | Where-Object { "$($_.Scope)" -like "$Scope" })
366333
}
@@ -381,14 +348,15 @@ https://github.com/DavidWhittingham/ps-EnablePython
381348
$pythons = ($pythons | Where-Object { "$($_.Platform)" -like "$Platform" })
382349
}
383350

351+
# Sort PythonCore first
384352
@(
385353
@($pythons | Where-Object { $_.Company -eq "PythonCore" } | Sort-Object -Property $sortProperties) +
386354
@($pythons | Where-Object { $_.Company -ne "PythonCore" } | Sort-Object -Property $sortProperties)
387355
)
388356
}
389357
}
390358

391-
function createPythonVersion([Microsoft.Win32.RegistryKey]$tagKey, [string]$scope) {
359+
function createPep514PythonVersion([Microsoft.Win32.RegistryKey]$tagKey, [string]$scope) {
392360
$parentKey = Get-Item $tagKey.PSParentPath
393361
$installPathKey = Get-Item (Join-Path $tagKey.PSPath "InstallPath")
394362
$pythonExecutable = getPythonCommand $installPathKey
@@ -429,14 +397,107 @@ function createPythonVersion([Microsoft.Win32.RegistryKey]$tagKey, [string]$scop
429397
return (Join-Path $this.InstallPath "Scripts")
430398
}
431399

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
436401

437402
$obj
438403
}
439404

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+
440501
function getPythonCommand([Microsoft.Win32.RegistryKey]$installPathKey) {
441502
# test for "ExecutablePath" folder
442503
if ((Get-ItemProperty -Path $installPathKey.PSPath) -match "ExecutablePath") {
@@ -454,11 +515,11 @@ function getPythonCommand([Microsoft.Win32.RegistryKey]$installPathKey) {
454515
}
455516

456517
function getPythonVersion([System.Management.Automation.ApplicationInfo]$executablePath) {
457-
if ($executablePath -eq $null) {
518+
if ($null -eq $executablePath) {
458519
$null
459520
}
460521
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())'
462523
}
463524
}
464525

@@ -467,4 +528,13 @@ function is64Bit {
467528
((Get-CimInstance Win32_OperatingSystem).OSArchitecture -match '64-bit')
468529
}
469530

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+
470540
Export-ModuleMember "*-*"

0 commit comments

Comments
 (0)