SoFunction
Updated on 2024-11-21

python3 implementation hides the command window at runtime.

Change the suffix .py to .pyw

After the change, you can see in the file properties: Python File (no console) (.pyw)

Show no console and no command window will pop up when you run it.

Supplementary: python package exe file and hide the CMD command window.

Installing pyinstaller in a virtual environment

pip install pyinstaller 

Packaging exe command: (specific commands online information a lot)

# Package 1 py file and hide the execution window
pyinstaller -F -w  
# Package a py file (-F) and hide the execution window (-w), replace the exe's ico icon (-i).
pyinstaller -F -w -i   

This way of packaging will save all the dependencies as source files in a folder, most of the time we still want to have only one exe file

Pack all dependent libraries in the folder into the exe:

# Merge into one exe (--onefile), replace exe icon (--icon=), py source file (), hide execution (-w)
pyinstaller --onefile --icon=   -w

Attention:

When all the dependent libraries are packaged into an exe and executed in a hidden CMD window, an error will occur, causing the program to fail to run properly, so it is necessary to use the

subprocessto executeCMDcommand。这种方式to executecmdcommand就不会出现程序错误。
import subprocess 
cmd = 'Your CMD commands'
res = (cmd, shell=True, stdin=, stdout=, stderr=)
 

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.