This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: the winapi fails to run shortcuts (because considers a shortcut not a valid Win32App)
Type: enhancement Stage: resolved
Components: Windows Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: Bernát Gábor, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-07-20 22:32 by Bernát Gábor, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg298752 - (view) Author: Bernát Gábor (Bernát Gábor) Date: 2017-07-20 22:32
Although python27.exe.lnk is callable from any command line tool, with any of its arguments the subprocess module thinks it's not a valid Win32 application.

Proof, let there be python27.exe.lnk be a shortcut to python.exe:

C:\Python27
λ ls python*
python.exe*  python27.exe.lnk*  pythonw.exe*

λ python.exe -c "import sys; print(sys.version)"
2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)]

C:\Python27
λ python.exe -c "import subprocess; print(subprocess.call(['./python.exe', '-c', '\"import sys; print(sys.version)\"']))"
0

C:\Python27
λ python27.exe -c "import subprocess; print(subprocess.call(['./python27.exe.lnk', '-c', '\"import sys; print(sys.version)\"']))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 168, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application
msg298754 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-07-20 23:26
subprocess.Popen calls WinAPI CreateProcess, which can execute PE/COFF executables and .BAT/.CMD batch scripts. It doesn't know anything about .LNK shell shortcuts. If CreateProcess fails, a Windows shell (e.g. CMD or PowerShell) tries ShellExecuteEx, which knows how to open a .LNK file. You can use shell=True for this in Python.

Directly supporting ShellExecuteEx would be a major enhancement. It would probably require a new keyword-only parameter, or at least a sentinel value for the existing `shell` parameter.
msg298775 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2017-07-21 06:50
For shell objects such as shortcuts you could use os.startfile which invokes ShellExecute under the covers
msg298782 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-07-21 08:32
That depends on the shortcut. I have some shortcuts that are designed to be run from the command line (e.g. python.lnk), which take arguments and inherit the working directory of the parent process. I add .LNK to PATHEXT so I can run them as commands without a filename extension. os.startfile doesn't allow passing command-line arguments to these shortcuts.
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75162
2021-02-25 17:38:42eryksunsetstatus: open -> closed
resolution: works for me
stage: resolved
2017-07-21 08:32:34eryksunsetmessages: + msg298782
2017-07-21 06:50:02tim.goldensetmessages: + msg298775
2017-07-20 23:26:29eryksunsetnosy: + eryksun

messages: + msg298754
versions: - Python 2.7, Python 3.3, Python 3.4, Python 3.5
2017-07-20 22:32:04Bernát Gáborcreate