简体   繁体   中英

Printing output of python script to Windows console, when running via batch file

I have tried to look at other resources on SO but none seem to solve my problem:

Executing Python Script From Command Line is Hiding Print Statements

How do I write output in same place on the console?

Python script doesn't print output in command prompt

I have a .py script that I'm executing via a batch file. Within my .py script, I have a few different attempts at printing output to the console window. When I run the batch file and the console window pops up, I don't see the print output. I don't have any other problems with executing the program, and I've otherwise used python and batch files to generate file output, etc.

This is my .py script, I'm trying three different ways of writing output based on other SO articles:

import sys

# Attempt 1
print('hello')

# Attempt 2
sys.stdout.write('hello')

# Attempt 3
if __name__ == '__main__':
    print('hello')

My batch file:

"C:\Program Files\Anaconda3\pythonw.exe"  "\\*********\printTest.py"
PAUSE

My output, none of my desired 'hello' output displays. I've edited the location of my script but the directory has been triple checked and is correct:

C:\Windows>"C:\Program Files\Anaconda3\pythonw.exe"  "\\********\printTest.py"

C:\Windows>PAUSE
Press any key to continue . . .

How do I get 'hello' to print to the console?

You're running "pythonw.exe", which is for graphical user interface (GUI) scripts, not console scripts. Run "python.exe" to inherit CMD's console for standard I/O.

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