Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Python script without Windows console appearing

Tags:

python

windows

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.

like image 927
David Avatar asked Feb 07 '23 12:02

David


2 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

like image 149
Ramón Gil Moreno Avatar answered Feb 13 '23 07:02

Ramón Gil Moreno


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:

  1. re-install/repair python installation (run installer, it will propose to repair install)
  2. if you're not administrator of your machine, you can associate .pyw files to pythonw.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\" %*"

like image 26
Jean-François Fabre Avatar answered Feb 13 '23 05:02

Jean-François Fabre