Building a Windows service

1,126 views
Skip to first unread message

Don Dwiggins

unread,
Apr 10, 2013, 7:34:01 PM4/10/13
to pyins...@googlegroups.com
Does anyone here have experience building a Windows service with PyInstaller?  I have one that I've been using py2exe on, but it's run into problems when I started using SqlAlchemy, and I'm thinking of switching, especially since I'm using PyInstaller for just about every other distributable.

Thanks,

--
Don Dwiggins
Advanced Publishing Technology

Zak

unread,
Apr 10, 2013, 8:05:25 PM4/10/13
to pyins...@googlegroups.com, ddwi...@advpubtech.com
Hello Don,

I hate Windows but I work with it a lot. I made something similar to a Windows service using PyInstaller and PySide. More specifically, I was making a backup utility. I wanted it to start every time Windows booted up, I wanted it to keep running while Windows was "asleep" and even if the user had been logged out due to inactivity, and I wanted it to not have a window. I achieved all these goals. Here is how I did it:

1. When calling PyInstaller, use the --windowed option. This seems counter-intuitive since we don't want a window, but bear with me. If you use --nowindowed that is definitely bad because it will open a CMD.EXE terminal that displays STDOUT from the Python process.

2. We are going to make this a PySide GUI application, at least in theory. Here is a working example:

# File: myapp.py

import threading

from PySide.QtCore import *
from PySide.QtGui import *

class SpecialWindow(QDialog):
    def __init__(self, parent=None):
        super(SpecialWindow, self).__init__(parent)
        self.setWindowTitle("Name of My Application")

        # Make the "minimize" button appear:
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        # Without the line above, the "minimize" button may
        # be grayed out on Windows.

special_window = SpecialWindow()
special_window.show()
special_window.showMinimized()
# Calling .showMinimized() is equivalent to clicking the
# minimize button in the corner of the window.

# Whatever your background application is supposed to do,
# put that Python code here. I did this using the Python
# threading library. Here is a little example:

thread = threading.Thread(group=None, target=my_awesome_cool_function, name="My thread")
thread.start()

app.exec_()

# Execution reaches this point when the user closes the application, usually by
# maximizing it and then clicking the X in the corner.

# Clean up your thread and then terminate it.

3. Package myapp.py with PyInstaller, and try it out. Double-clicking myapp.exe should start your Python code, a GUI window will NOT open, and an icon appears in the system tray. When you mouse over the icon, it will say "Name of My Application", or whatever "window title" you set. If you click it, a GUI window will open. You can close this GUI window and thus terminate the app. Alternatively, you can re-minimize the app by clicking in the corner.

4. To make this background application start every time Windows boots up, place myapp.exe in the startup folder. On Windows 8, the startup folder is here:

C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

This should work, I have done it recently.

Good luck,

Zak Fallows

Don Dwiggins

unread,
Apr 10, 2013, 10:23:16 PM4/10/13
to pyins...@googlegroups.com
On 4/10/13 5:05 PM, Zak wrote:
Hello Don,

I hate Windows but I work with it a lot.

Yep; me too.


I made something similar to a Windows service using PyInstaller and PySide. More specifically, I was making a backup utility. I wanted it to start every time Windows booted up, I wanted it to keep running while Windows was "asleep" and even if the user had been logged out due to inactivity, and I wanted it to not have a window. I achieved all these goals. Here is how I did it:

Very nice.  I want to stay with the windows service to make it easier to control from the Services panel.  Still, I'll keep your idea in mind.  Thanks for the suggestion.

--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To post to this group, send email to pyins...@googlegroups.com.
Visit this group at http://groups.google.com/group/pyinstaller?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Martin Zibricky

unread,
Apr 11, 2013, 8:09:07 AM4/11/13
to pyins...@googlegroups.com
Don Dwiggins píše v St 10. 04. 2013 v 16:34 -0700:
> Does anyone here have experience building a Windows service with
> PyInstaller? I have one that I've been using py2exe on, but it's run
> into problems when I started using SqlAlchemy, and I'm thinking of
> switching, especially since I'm using PyInstaller for just about every
> other distributable.

A winodws service is a not well maintained area in pyinstaller:
- I have never used it myself.
- Not sure how it should work.
- Not sure if this feature of pyinstaller still works.

Anyone else has to speak out about this topic.


Don Dwiggins

unread,
Apr 11, 2013, 12:21:36 PM4/11/13
to pyins...@googlegroups.com
On 4/11/13 5:09 AM, Martin Zibricky wrote:
> A winodws service is a not well maintained area in pyinstaller:
> - I have never used it myself.
> - Not sure how it should work.
> - Not sure if this feature of pyinstaller still works.

OK. If I can't beat py2exe into submission, and nobody else speaks up
here, I may try to tackle this.

Don Dwiggins

unread,
May 7, 2013, 11:54:17 AM5/7/13
to pyins...@googlegroups.com
On 4/11/13 9:21 AM, Don Dwiggins wrote:
On 4/11/13 5:09 AM, Martin Zibricky wrote:
A winodws service is a not well maintained area in pyinstaller:
- I have never used it myself.
- Not sure how it should work.
- Not sure if this feature of pyinstaller still works.

OK.  If I can't beat py2exe into submission, and nobody else speaks up here, I may try to tackle this.

Here's a followup that might be useful, in case anyone decides to tackle this.

I've finally gotten to the root of the problem with py2exe, guided by Mark Hammond.  In the process, I've learned more than I ever wanted to know about how Python-written services work.  I'm not going to take on porting what I know to PyInstaller -- I just don't have the time.  However, here's what I'd suggest for someone who does have the time and passion for it:
  • Get a copy of "Python Programming on Win32", by Hammond & Robinson; study Chapter 18, specifically the sections on writing services.  (You might find the relevant material online.)
  • Get py2exe and pywin32.  Study the modules py2exe.boot_service and win32service,win32serviceutil.
  • Using "vanilla" PyInstaller, try to fold boot_service (or the relevant code from it) into a simple service that will successfully install, start, serve, stop, and remove (the basic Service Manager functions).
  • With success there, come up with a combination of stuff folded into PyInstaller itself, and a template for writing services that can be frozen with PyInstaller.

Hope this helps someone sometime,

Reply all
Reply to author
Forward
0 new messages