views:

84

answers:

2

The way I currently add an executable for my Python-based GUI is this:

setup(
      # ...
      entry_points = {"gui_scripts" : ['frontend = myfrontendmodule.launcher:main']},
      # ...
      )

On Windows, this will create "frontend.exe" and "frontend-script.pyw" in Python's scripts folder (using Python 2.6). When I execute the EXE file, a console window is shown but the PYW file works correctly without showing one.

So my question is: How can I make the EXE file execute the program without the console window? The solution should work on Linux, too (don't suggest py2exe ;).

A: 

Why don't you use the .pyw file for Linux and py2exe for Windows?

leoluk
Because on http://packages.python.org/distribute/setuptools.html the changelog says "0.6a3 - Added gui_scripts entry point group to allow installing GUI scripts on Windows and other platforms. (The special handling is only for Windows; other platforms are treated the same as for console_scripts.)". So I thought they made some changes to adapt it for Windows. And I have version 0.6c11 which is newer.
AndiDog
+2  A: 

Alright, I investigated a bit in the setuptools source code and it all boils down to a bug in setuptools (easy_install.py):

# On Windows/wininst, add a .py extension and an .exe launcher
if group=='gui_scripts':
    ext, launcher = '-script.pyw', 'gui.exe'
    old = ['.pyw']
    new_header = re.sub('(?i)python.exe','pythonw.exe',header)
else:
    ext, launcher = '-script.py', 'cli.exe'
    old = ['.py','.pyc','.pyo']
    new_header = re.sub('(?i)pythonw.exe','python.exe',header)

if os.path.exists(new_header[2:-1]) or sys.platform!='win32':
    hdr = new_header
else:
    hdr = header

The last if statement decides whether pythonw.exe's or python.exe's path is written into the shebang of "frontend-script.pyw". As this shebang is evaluated by the created EXE file, it is necessary that the else statement is not executed. The problem is that new_header[2:-1] in my case was "C:\Program Files (x86)\Python26\pythonw.exe" (with the quotes!), so os.path.exists said it does not exist because of the quotes.

I will try to get this corrected by the setuptools developers. The remaining problem will be the absolute pythonw.exe path. If I create a Windows setup/MSI installer, the shebang will contain my pythonw.exe path ("C:\Program Files (x86)\Python26\pythonw.exe") but the user might have installed Python in "C:\Python26". I'll report the final solution after I've reported this problem.

AndiDog
Thanks! I've put a fix in SVN for you (that does a .strip('"') on the path used in the exists() call), and it'll be available in tomorrow's snapshot version. Use "easy_install -U setuptools==dev06" to upgrade to the SVN version now and give it a try!
pjeby
@pjeby: Cool, that was even before I could use my newly created bug tracker account. Thanks, I'll try it as soon as possible.
AndiDog
@pjeby: I filed the bug with the absolute paths at http://bugs.python.org/setuptools/issue112
AndiDog