0

I needed to install Python 3.6.1 x64 for a project on windows. I uninstalled 3.6.1 x86 and installed 3.6.1 x64. I made sure that my PATH env var is pointing to the new install. I then associated py files with the correct python.exe: C:\Program Files\Python36\python.exe instead of: C:\Program Files (x86)\Python36-32\python.exe

I can run a script directly from a cmd window by calling python no problem: python myscript.py

but if I double click it displays a cmd windows bot nothing in it.

If I "Open with" it won't load, just giving me the waiting cursor for a couple of seconds. Even after I browsed for the new python.exe

What am I missing here?

Everything was working fine for Python x86

Thanks!

Geordie
  • 1,132
  • 14
  • 24
  • In the Control Panel "Default Programs" dialog for setting file associations, ensure that .py files are associated with "Python" from the Python Software Foundation, with description "Python File". The icon should have the Python logo, possibly with a rocket on it if you've installed the py launcher. – Eryk Sun Apr 21 '17 at 02:10
  • Yup it's assocaited with the launcher (python icon with rocket). I just did a full uninstall of 3.6.1 x64, manually deleted all python install folders and reinstalled x64. I get the same thing. It looks like a problem with the launcher (py.exe) not python – Geordie Apr 21 '17 at 02:30

1 Answers1

1

Le sigh, two issues happening. Something was borked with the Python install somewhere, a complete uninstall and reinstall of 3.6.1 x64 fixed that for general python execution.

Secondly, I'm using Pywinauto which is why I need x64 to access x64 app's UI. Apparently it also needs elevated user rights ie admin. So if I double click a script it just hangs with no feedback, errors etc because the launcher isn't run with admin rights.

I didn't catch this because I was always executing python manually from an elevated cmd window. If I find a way to always execute the launcher as admin I'll update here.

UPDATE: Here's an in depth thread on getting Python to elevate execution privileges on Windows: How to run python script with elevated privilege on windows

It was far easier for me to write a Powershell script to do this job:

# ========================================= Admin Rights =======================================================
function asAdmin
{
    [string]$cmdPath = $args[0]
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$cmdPath`"" -Verb RunAs; exit }
}

asAdmin $PSCommandPath

Start-Process "pythonw" "$PSScriptRoot\myPythonScriptWithUI.pyw"
Community
  • 1
  • 1
Geordie
  • 1,132
  • 14
  • 24
  • 1
    Do you mean you want a particular script to execute elevated, or that you always want py.exe to run elevated? The latter is possible by forcing running as administrator in the compatibility properties for py.exe. Under the hood, this gets set in `HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers` as a value with the full path to the executable and the data `~ RUNASADMIN`. – Eryk Sun Apr 21 '17 at 03:03
  • 1
    If it's the former, the script can `ShellExecuteEx` itself with the "runas" verb. Use the "Ex" version instead of `ShellExecute` to be able to get a process handle. Then call `WaitForSingleObject` to wait for the elevated instance to exit and `GetExitCodeProcess` to proxy its exit code as your own. You can call these functions via PyWin32 or ctypes, or a custom extension module. – Eryk Sun Apr 21 '17 at 03:05
  • Just particular scripts. Yup I saw a thread here about getting Python script to run itself as admin, but for me it's far easier to just have a launcher Powershell script that does the same thing then launches Python. – Geordie Apr 24 '17 at 18:28