Run Python script without Windows console appearing
pythonw.exe will run the script without a command prompt. The problem is that the Python interpreter, Python.exe, is linked against the console subsystem to produce console output (since that's 90% of cases) -- pythonw.exe is instead linked against the GUI subsystem, and Windows will not create a console output window for it unless it asks for one.
This article discusses GUI programming with Python, and also alludes to pythonw.exe. It also helpfully points out that if your Python files end with .pyw
instead of .py
, the standard Windows installer will set up associations correctly and run your Python in pythonw.exe.
In your case it doesn't sound like a problem, but reliance upon pythonw.exe makes your application Windows-specific -- other solutions exist to accomplish this on, say, Mac OS X.
How to run a Python script without Windows console appearing
Use ShellExecuteEx function.
BOOL ShellExecuteEx(
_Inout_ SHELLEXECUTEINFO *pExecInfo
);
This is the pExecInfo:
***nShow - Flags that specify how an application is to be shown when it is opened
typedef struct _SHELLEXECUTEINFO {
DWORD cbSize;
ULONG fMask;
HWND hwnd;
LPCTSTR lpVerb;
LPCTSTR lpFile;
LPCTSTR lpParameters;
LPCTSTR lpDirectory;
int nShow;/*=0 if you don't want the console window to appear*/
HINSTANCE hInstApp;
LPVOID lpIDList;
LPCTSTR lpClass;
HKEY hkeyClass;
DWORD dwHotKey;
union {
HANDLE hIcon;
HANDLE hMonitor;
} DUMMYUNIONNAME;
HANDLE hProcess;
} SHELLEXECUTEINFO, *LPSHELLEXECUTEINFO;
Hiding the console while executing a script on Windows
The way I've done it on Windows 7 is by making a shortcut (or link) that runs my script with the pythonw.exe
interpreter, which has no console, instead of the default python.exe
.
Just follow these 3 steps:
- First create a normal shortcut to your script. One way to do this is to drag the icon for the script file shown in an Explorer window to where you want the shortcut to be (like the desktop) and hold down the Alt key when releasing the mouse button.
- Right-click on the just created shortcut and select
Properties
from the the menu that pops-up. - A Properties dialog for the shortcut will appear. In it insert
C:\python27\pythonw.exe
and a space before the path to the your script file. if the path to your script has any spaces in it, it must now be enclosed in double quotes._ If you're using another version of Python, you'll also need to change thePython27
accordingly.
i.e. A target of D:\path with spaces in it to\myscript.py
would need to be changed
to C:\Python27\pythonw.exe "D:\path with spaces in it to\myscript.py"
You can also change the shortcut's icon here if you wish.
Here's an example:
Update - simpler way: You can also change the extension of your script to .pyw
which will cause it to be run with the pythonw.exe
instead of the python.exe
.
Is there a way to remove the extra window when running a python program with pygame?
By renaming your .py file (for example 'main.py') to .pyw and then converting it to an executable the same way you did it last time, you could avoid opening the console window.
Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.
From the python documentation.
Related Topics
What Does a . in an Import Statement in Python Mean
Moving Matplotlib Legend Outside of the Axis Makes It Cutoff by the Figure Box
Generate a Random Date Between Two Other Dates
Not Letting the Character Move Out of the Window
How to See If There's an Available and Active Network Connection in Python
Returning the Product of a List
How to Intercept Calls to Python's "Magic" Methods in New Style Classes
Why Is My Pygame Display Not Responding While Waiting for Input
Large, Persistent Dataframe in Pandas
How to Print a Generator Expression
Calling Filter Returns <Filter Object at ... >
String Concatenation Without '+' Operator
Sorting Columns in Pandas Dataframe Based on Column Name
How to Write Png Image to String with the Pil
Execute Multiple Commands in Paramiko So That Commands Are Affected by Their Predecessors