3

Using python 2.7.11 on Windows 10 64-bit Home Premium upgraded in place from Windows 7

In a standard console, the following code is saved as hello.py and prints: Hello World When invoked with python hello.py

#! python
print 'Hello World'

It produces no console output at all when invoked with hello.py or hello

The program runs in all three cases.
I can demonstrate this by adding

with open('world.txt', 'w') as fh:
    fh.write('dolly\n')

Each of the three methods of invoking the program all successfully write world.txt with the correct information.

python.exe is in my path
.py is in the pathext variable
.py is associated with python.exe, and the correct command line is in the "shell open" registry entry.

all of the above is in accordance with
https://docs.python.org/2/faq/windows.html
I had no trouble running python scripts from the command line without the extension in Windows 7

Thanks,

Vorpal Swordsman
  • 393
  • 1
  • 5
  • 14
  • Update: trying sys.stdout.write() and stderr.write() show same behaviour. Running with elevated privileges doesn't help – Vorpal Swordsman May 26 '16 at 04:37
  • Write the actual command line into the file: `subprocess.call('wmic process where processid=%d get commandline' % os.getpid(), stdout=fh)`. – Eryk Sun May 26 '16 at 04:47
  • I thought I had found an answer: http://stackoverflow.com/questions/3018848/cannot-redirect-output-when-i-run-python-script-on-windows-using-just-scripts-n which points to: https://support.microsoft.com/en-us/kb/321788 My registry already has the correct value – Vorpal Swordsman May 26 '16 at 04:50

1 Answers1

1

On a whim, I navigated to the registry key specified in my previous comment.
The instruction here: https://support.microsoft.com/en-us/kb/321788
Say to add a REG_DWORD value "InheritConsoleHandles" to the key
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
and set its value to 1

I set its value to 0 and opened a new command prompt. Subsequently, running python scripts without their extensions "just works"

I love whims

Vorpal Swordsman
  • 393
  • 1
  • 5
  • 14
  • Did your previous Windows 7 installation have this setting? It's not configured in a fresh install of Windows 10. I added it and can confirm that it breaks console I/O when running a script via `ShellExecuteEx` instead of directly calling `CreateProcess` to run python.exe. So I ran `cdb -o cmd` to debug a command prompt and all child processes. With this setting, the python.exe process is created with its standard handles set to `NULL`. A lot of the underlying console API has been rewritten in Windows 8/10. New code means new bugs. – Eryk Sun May 26 '16 at 06:19
  • Yes, I am nearly certain it did. No W10 installation I checked today had this key and value – Vorpal Swordsman May 27 '16 at 02:39
  • This is the kind of obscure setting that someone might configure and forget about a few months later, only to be faced with this baffling bug after upgrading to Windows 10. – Eryk Sun May 27 '16 at 02:53