WindowsApps loads Python install instead of user's installed Python
Question:
As a result of corporate policy users can’t edit their paths, cannot change environment variables.
I figured out via VBA to alter the USER paths.
There’s one issue. All the installations have, in the SYSTEM environment variables:
%USERPROFILE%AppDataLocalMicrosoftWindowsApps
The PATH of a script session will take the SYSTEM paths and append the USER paths to it. Pretty normal Windows behavior.
The issue I run into after setting paths is the ORDER causes issues. WindowsApps has Python as an installation package, so if it comes first in the PATH statement, on some PCs it tries to load the Python install, rather than the user’s installed Python.
I have developer rights so I have control of my laptop but my end users do not. If there wasn’t a company lockdown, I’d have users remove the WindowsApps from their SYSTEM PATH.
The normal full directory paths:
%USERPROFILE%Anaconda3;
%USERPROFILE%Anaconda3Library;
%USERPROFILE%Anaconda3Librarybin;
%USERPROFILE%Anaconda3Scripts;
The code:
Sub RunPythonScript()
Dim objShell As Object
Dim PythonExePath, PythonScript As String
Set objShell = VBA.CreateObject("Wscript.Shell")
Dim WaitOnReturn As Boolean
Dim WindowStyle As Integer
WindowStyle = 1
WaitOnReturn = True
' find the Python Path in the workbook
PythonPath_Row = ThisWorkbook.Sheets(ActiveSheet.Name).Columns(1).Find(What:="Python Path", LookIn:=xlValues).Row + 1
PythonPath = ThisWorkbook.Sheets(ActiveSheet.Name).Cells(PythonPath_Row, 1)
' set only the User environment settings since no admin permission required
Set colUserEnvVars = objShell.Environment("User")
' save the original PATH
originalPATH = colUserEnvVars.Item("Path")
' add the needed Python directories to the PATH
colUserEnvVars.Item("PATH") = PythonPath & ";" & PythonPath & "Library" & ";" & PythonPath & "Librarybin" & ";" & PythonPath & "Scripts" & ";" & colUserEnvVars.Item("Path")
PythonScript = """" & ThisWorkbook.Path & "MyScript.py" & """"
PythonEXE = PythonPath & "python.exe"
' run Python script
objShell.Run PythonEXE & " " & PythonScript, WindowStyle, WaitOnReturn
' put path back to normal
colUserEnvVars.Item("PATH") = originalPATH
End Sub
I am explicitly calling Python from it’s installation directory.
I step through the VBA code and verify the command line passed to the shell.
I opened a CMD prompt after setting the path and it’s all correctly modified.
Why are some PCs opening Python from WindowsApps?
Answers:
Please, try the next function to obtain the installation path of any (installed) application which open a specific extension (as default):
Private Function GetExePath(ext As String) As String
Dim strAppl As String, strPathExe As String, strExeFile As String
Dim WSHShell As Object
Set WSHShell = CreateObject("WScript.Shell")
On Error Resume Next
strAppl = WSHShell.RegRead("HKEY_CLASSES_ROOT" & WSHShell.RegRead("HKEY_CLASSES_ROOT" & ext & "") & _
"shellopencommand")
If err.Number <> 0 Then
err.Clear: On Error GoTo 0
GetExePath = ""
MsgBox "No program installed for extension """ & ext & """"
Exit Function
End If
On Error GoTo 0
strExeFile = Split(strAppl, """ """)(0)
strExeFile = Right(strExeFile, Len(strExeFile) - 1)
GetExePath = strExeFile
End Function
You can test it in the next way:
Private Sub testGetExeP()
Debug.Print GetExePath(".xls")
Debug.Print GetExePath(".pdf")
Debug.Print GetExePath(".py")
End Sub
Please, try comparing the path from the used workbook with the returned exe path. Firstly, to check if my supposition (from the above comment) is confirmed and secondly to use the function instead of the path extracted from any workbook.
As a result of corporate policy users can’t edit their paths, cannot change environment variables.
I figured out via VBA to alter the USER paths.
There’s one issue. All the installations have, in the SYSTEM environment variables:
%USERPROFILE%AppDataLocalMicrosoftWindowsApps
The PATH of a script session will take the SYSTEM paths and append the USER paths to it. Pretty normal Windows behavior.
The issue I run into after setting paths is the ORDER causes issues. WindowsApps has Python as an installation package, so if it comes first in the PATH statement, on some PCs it tries to load the Python install, rather than the user’s installed Python.
I have developer rights so I have control of my laptop but my end users do not. If there wasn’t a company lockdown, I’d have users remove the WindowsApps from their SYSTEM PATH.
The normal full directory paths:
%USERPROFILE%Anaconda3;
%USERPROFILE%Anaconda3Library;
%USERPROFILE%Anaconda3Librarybin;
%USERPROFILE%Anaconda3Scripts;
The code:
Sub RunPythonScript()
Dim objShell As Object
Dim PythonExePath, PythonScript As String
Set objShell = VBA.CreateObject("Wscript.Shell")
Dim WaitOnReturn As Boolean
Dim WindowStyle As Integer
WindowStyle = 1
WaitOnReturn = True
' find the Python Path in the workbook
PythonPath_Row = ThisWorkbook.Sheets(ActiveSheet.Name).Columns(1).Find(What:="Python Path", LookIn:=xlValues).Row + 1
PythonPath = ThisWorkbook.Sheets(ActiveSheet.Name).Cells(PythonPath_Row, 1)
' set only the User environment settings since no admin permission required
Set colUserEnvVars = objShell.Environment("User")
' save the original PATH
originalPATH = colUserEnvVars.Item("Path")
' add the needed Python directories to the PATH
colUserEnvVars.Item("PATH") = PythonPath & ";" & PythonPath & "Library" & ";" & PythonPath & "Librarybin" & ";" & PythonPath & "Scripts" & ";" & colUserEnvVars.Item("Path")
PythonScript = """" & ThisWorkbook.Path & "MyScript.py" & """"
PythonEXE = PythonPath & "python.exe"
' run Python script
objShell.Run PythonEXE & " " & PythonScript, WindowStyle, WaitOnReturn
' put path back to normal
colUserEnvVars.Item("PATH") = originalPATH
End Sub
I am explicitly calling Python from it’s installation directory.
I step through the VBA code and verify the command line passed to the shell.
I opened a CMD prompt after setting the path and it’s all correctly modified.
Why are some PCs opening Python from WindowsApps?
Please, try the next function to obtain the installation path of any (installed) application which open a specific extension (as default):
Private Function GetExePath(ext As String) As String
Dim strAppl As String, strPathExe As String, strExeFile As String
Dim WSHShell As Object
Set WSHShell = CreateObject("WScript.Shell")
On Error Resume Next
strAppl = WSHShell.RegRead("HKEY_CLASSES_ROOT" & WSHShell.RegRead("HKEY_CLASSES_ROOT" & ext & "") & _
"shellopencommand")
If err.Number <> 0 Then
err.Clear: On Error GoTo 0
GetExePath = ""
MsgBox "No program installed for extension """ & ext & """"
Exit Function
End If
On Error GoTo 0
strExeFile = Split(strAppl, """ """)(0)
strExeFile = Right(strExeFile, Len(strExeFile) - 1)
GetExePath = strExeFile
End Function
You can test it in the next way:
Private Sub testGetExeP()
Debug.Print GetExePath(".xls")
Debug.Print GetExePath(".pdf")
Debug.Print GetExePath(".py")
End Sub
Please, try comparing the path from the used workbook with the returned exe path. Firstly, to check if my supposition (from the above comment) is confirmed and secondly to use the function instead of the path extracted from any workbook.