Create a startup GUI script from Python setuptools (without a console window!)

I am currently adding an executable for my Python based GUI:

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

On Windows, this will create "frontend.exe" and "frontend-script.pyw" in the Python scripts folder (using Python 2.6). When I run the EXE file, the console window is displayed, but the PYW file is working correctly without showing it.

So my question is: how can I get an exe file to execute a program without a console window? The solution should also work on Linux (do not offer py2exe;).

+7
python setuptools distutils
source share
2 answers

Well, I did a little research on the setuptools source code, and it all boils down to the error 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 decides whether the path pythonw.exe or python.exe is written to shebang "frontend-script.pyw". Since this shebang is evaluated by the created EXE file, it is necessary that the else not executed. The problem is that new_header[2:-1] in my case was "C: \ Program Files (x86) \ Python26 \ pythonw.exe" (with quotes!), Therefore os.path.exists said that it does not exist because of quotes.

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


I posted this two years ago, I'm sorry I have not yet proposed my solution. Not sure if there is a more modern solution (maybe distribute offers something), but here is what I used then (copying)

dogsync-frontend-script.pyw

 #!pythonw.exe # This script will be executed by the primary Python version that is installed, which might as well be Python 3. But # we want to execute it with the Python version that belongs to this script path. So let do a major hack: import os import sys import subprocess if sys.argv[-1] == "magic": from dogsync_frontend.launcher import main main() else: # The CPython folder hierarchy is assumed here (<installation>\pythonw.exe, <installation>\Scripts\<thisscript>) subprocess.Popen([os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "pythonw.exe")), __file__, "magic"]) 

dogsync-frontend.exe file

Automatically copied from <python installation>\lib\site-packages\setuptools\gui.exe (see below). This file will automatically execute script <name of EXE>-script.py[w] if I remember correctly.

Setup.py setup.py

 from setuptools import __file__ as setupToolsFilename if os.name == "nt": # Use a customized (major hack) start script instead of the one that gets automatically created by setuptools # when the "gui_scripts" parameter is used. This way, we don't need setuptools installed in order to run DogSync. shutil.copy2(os.path.join(os.path.dirname(setupToolsFilename), "gui.exe"), "build-environment/windows-scripts/dogsync-frontend.exe") startScripts = dict(scripts = ["build-environment/windows-scripts/dogsync-frontend-script.pyw", "build-environment/windows-scripts/dogsync-frontend.exe"]) else: # For Linux, I don't have a solution to remove the runtime dependency on setuptools (yet) startScripts = dict(entry_points = {"gui_scripts" : ['dogsync-frontend = dogsync_frontend.launcher:main']}) setup(<other options>, **startScripts) 

With this installation, exe / pyw files are copied to <python installation>\Scripts (on Windows), and running dogsync-frontend.exe will run the pyw script without a console. Since setuptools has not received any updates for many years, this solution still works.

+10
source share

Why aren't you using a .pyw file for Linux and py2exe for Windows?

0
source share

All Articles