Is there any way to run a Python script without a command shell momentarily appearing? Naming my files with the ".pyw" extension doesn't work.
4 Answers
Try using the Python's pythonw.exe
executable to start your script.
In Windows OSes, executables that are console applications (python.exe
with out the w
is a console app) are run showing a console window; in the other hand regular Windows applications do not spawn that black console window.
You can find the details about these two executables in this old question: pythonw.exe or python.exe?
And about Windows different types of applications here: Difference between Windows and Console application

- 1
- 1

- 809
- 5
- 19
-
Thanks,Now I am running the python file from c like this: system("python.pyw"). How should I execute it now? – David Jul 27 '16 at 13:21
-
1@David, `system` uses cmd to run a program, which briefly flashes a console when you run a non-console program from a non-console program. If you're not passing parameters to the script you can use `os.startfile("script.pyw")` instead. But first fix the .pyw file association. The default value of `[HKLM|HKCU]\Software\Classes\.pyw` should be `Python.NoConFile`. Then use the right-click "open with" dialog to select to always use Python for .pyw scripts. If there are multiple Python entries, try them all until it works right, but *do not* "Look for another app on this PC". – Eryk Sun Jul 27 '16 at 17:44
In all python installs since 2.5 (and possibly earlier), if the install has been done properly, .py
files are associated to python.exe
and .pyw
files are associated to pythonw.exe
If the associations have been tampered with, or overridden for a particular user, that may be different.
Run the following command in a cmd:
ftype | find "pythonw"
assoc | find ".pyw"
I get:
Python.NoConFile="D:\Program Files\Python27\pythonw.exe" "%1" %*
.pyw=Python.NoConFile
If you don't have that, you can do several things to fix that:
- re-install/repair python installation (run installer, it will propose to repair install)
if you're not administrator of your machine, you can associate
.pyw
files topythonw.exe
. Minor problem with that, you have to alter the registry key afterwards to add the extra arguments or dropping a parameter on your .pyw file won't take it into account (it's seldom used but still)[HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command] @="\"L:\\Portable_Python_2.7.3.1\\App\\pythonw.exe\" \"%1\" %*"

- 137,073
- 23
- 153
- 219
-
It's best to modify `HKLM\Software\Classes` or `HKCU\Software\Classes`. The `HKCR` virtual key is a merged view of these two trees, so the result of modifying it depends on the currently defined keys. Merging a .reg file that uses `HKCR` may modify the subkey in `HKCU` if it exists, else the subkey in `HKLM` if it exists, else create the subkey in `HKLM`. So a key and its subkeys could end up partially defined for the current user and partially defined for the local machine. – Eryk Sun Jul 27 '16 at 18:24
-
Note that cmd's `assoc` and `ftype` commands only show and modify the `HKLM\Software\Classes` keys and these keys only define one possibility for the user's selection in Explorer. There are other places file associations can be defined, such as `[HKCU|HKLM]\Software\Classes\Applications` and defaults under `[HKCU|HKLM]\Software\Classes\SystemFileAssociations`. The user's current selection is in `HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts`, but this can [should] only be modified using the GUI. – Eryk Sun Jul 27 '16 at 18:34
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;

- 733
- 2
- 11
- 30
The simple answer is to copy the "LazyLibrarian.py" to "LazyLibraryian.pyw" and create a shortcut to Desktop. Then put the shortcut in your startup folder.

- 185
- 1
- 6