Run Python Script Without Windows Console Appearing

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:

  1. 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.
  2. Right-click on the just created shortcut and select Properties from the the menu that pops-up.
  3. 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 the Python27 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:

screenshot of filled in shortcut properties dialog

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



Leave a reply



Submit