简体   繁体   中英

Running an interactive Python script from a Batch File

I am wanting to run an interactive Python Program from a Batch File. I found the answer to the question "pythonw.exe or python.exe?" helpful, but not all the issues I had in mind were resolved. I decided to experiment using con: .

The following demonstrates the kind of interaction I have achieved:

Microsoft Windows [Version 10.0.18362.476]
(c) 2019 Microsoft Corporation. All rights reserved.

c:\sjt\PY\NEWER>type call_py.bat

@echo off
call python c:\sjt\py\newer\testout.py 1>con: 2>con:
c:\sjt\PY\NEWER>type testout.py
print ("Print works if you can see this.")
strwaiter = raw_input ("raw_input prompt: ")
print ("This string was received by strwaiter in response to the prompt: " + strwaiter)

c:\sjt\PY\NEWER>call_py

Print works if you can see this.
raw_input prompt: Here is my response.
This string was received by strwaiter in response to the prompt: Here is my response.

c:\sjt\PY\NEWER>

I tried running call_py.bat again but with it calling pythonw instead of python , this attempt did not produce the desired result.

Also, during my experimenting, I tried calling python without the redirection of 1 and 2 . This, likewise, was unsuccessful.

I attempted to add a comment to the relevant answer to that question, but failed because I do not have the required reputation. I am posting this question instead.

  1. Does my experiment add anything to the answers to that question?

  2. Given that I know nothing about the technical details given in that post, why is it that calling python in my batch file works (with these redirections) but calling pythonw doesn't?

PS See start /? about differences in starting types of exe files.

There are two types of programs in Windows. Graphical and console, or another way of saying it is console and non-console. The difference is that console programs automatically get or inherit (if there is an existing one) a console window. And thus get access to StdIn etc (which don't exist in the non console world).

If a program gets a console or not is controlled by a flag in the program file's header.

Typically console programs act as console programs, but they don't have too. They can have windows if they want.

Typically GUI programs don't do console stuff, but if they want they can attach to their parent's console and act as a console programs.

Programs without any UI are GUI programs. GUI program just means no console. If a program wants windows it has to create them.

To give you an idea. If you were to take Notepads's source code and compile it as a console program (by changing an option in the compiler). When you start it a console window will open (or it will inherit the current one) and the normal Notepad window will open. As Notepad doesn't have any code to interact with a console the console will just sit there.

One use for writing graphical programs and compiling them as console is that you use the console window for debugging information. Also if the program crashes error information is written to the console (rather than some deeply buried Problem Report that takes 50 clicks to get to).

The other thing to note is that Windows fully communicates to programs via a window. Console programs have to have threads to process messages and receive very limited messages mainly about the console closing or the user closed your program.

Windows messages are many. Even programs without a user interface will typically create a hidden window to receive messages (like shutdown, sleep, wallpaper changed, USB drive arrived, close program, etc).

On my computer with 4 apps visible I have a total of 410 windows.

https://winsourcecode.blogspot.com/2019/05/winlistexe-list-open-windows-and-their.html

This is Microsoft's Documentation of processes.

https://docs.microsoft.com/en-us/windows/console/about-character-mode-applications

https://docs.microsoft.com/en-us/windows/win32/procthread/processes-and-threads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM