System setups:
- Development PC: Win 10,Python 3.7.7, PyInstaller 5.3.12
- Deployment PC: Win11, No Python installled
I’ve made a CLI python script (running from Windows CMD) with the following simple logic:
- When started, open a TKinter’s “Select file” GUI dialogue wiht
filedialog.askopenfilename()
- Once .xlsx file has been selected, open MSExcel with
win32com.client.Dispatch('Excel.Application')
- Then enter a while(1) loop in the background, waiting for the user to press Spacebar
- When user presses spacebar, write a number to Excel’s active cell with
ExcelApp.ActiveCell.Value = <some number>
- Move the active cell “cursor” inside Excel down by 1 row with
ExcelApp.ActiveCell.GetOffset(1,0).Select()
When ran as a .py with python myScript.py
, it works 100% as intended without errors. Script could even write values to .xlsx files that user opens from OneDrive integration folders. All good.
However, the Win11 PC that the script is supposed to be deployed to has no Python installed. Let’s assume that Python cannot be installed on that PC at all due to i.e. no admin rights.
Therefore I packaged the script as a standalone .EXE with Pyinstaller using:
python -m PyInstaller --noconfirm --onedir --console "myScript.py"
The deployment to the Win11 pc was done in January, 2024. The packed .exe version of the script worked absolutely fine without ANY problems throughout the entire year.
Then, on 27th of December 2024 it decides to randomly out of the blue throw the following exception:
File "win32comclientdynamic.py line 627, in _getattr__
pywintypes com_error: (-2147418111, 'Call was rejected by callee, none, none)
I immediately run a number of tests:
- Copy the myScript.exe from Deployment to Dev machine and run it there. PASS.
- Re-build the last version of the .py script with PyInstaller on the Dev Machine, then copy .exe to deployment PC and run. FAIL! Same Exception.
- Re-build the last version of the .py script with PyInstaller on the Dev Machine, then copy it to deployment PC and run it with CMD. PASS
The pattern seemed totally random and non-deterministic. I was considering starting a prayer to please the Machine Spirits when I noticed, that the .exe runs inside something called a “Terminal”.
Now, it must be said I haven’t worked with Win 11 that much. Digging some more, I figured out that:
- “Terminal” is a “shell-shell” that is used by Win11 to run whatever would be ran inside CMD in 7/10.
- Win11’s Terminal starts PowerShell by default.
- Powershell is the reason the exception occurs, as it does NOT occur if .exe is ran with CMD.
I’ve quickly made a “fix” in a form of a .bat file that runs the .exe inside a CMD. But that is not the proper solution.
What are the reasons for:
- Win11 Terminal using Powershell by default for CLI apps? Powershell and CMD are very different by nature, and compatibility is not guaranteed…
- Powershell causing the “Call rejected by callee” exception in win32com? Security/execution policy? I’ve found similar question here, but it does the workaround only and does not provide the details as to why.