{"id":3704,"date":"2014-03-29T08:14:56","date_gmt":"2014-03-29T08:14:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/problems-involving-py2exe-python-services-collection-of-common-programming-errors\/"},"modified":"2014-03-29T08:14:56","modified_gmt":"2014-03-29T08:14:56","slug":"problems-involving-py2exe-python-services-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/problems-involving-py2exe-python-services-collection-of-common-programming-errors\/","title":{"rendered":"Problems involving py2exe &amp; python services-Collection of common programming errors"},"content":{"rendered":"<p>My goal is to create a single (or mutliple) .exe that can be used to install a Windows system service. Currently this is just a proof of concept so all my service does is write to a file to confirm it&#8217;s existence.<\/p>\n<p>When I invoke the service from a .py file it installs and runs fine. When I make a .exe out of it using py2exe, it will run fine, however I do get some messages in the Windows Event Log (Application) that certain libraries are not found. Sometimes windows stops the service after startup, sometimes windows ignores the &#8220;module not found&#8221; errors. All of this takes place on a XP SP3 machine.<\/p>\n<p>When I move the same .exe compiled w\/py2exe to a Win 7 SP1 machine, win7 informs me that the .exe cannot run without python27.dll. So I move python27.dll in the cwd of the .exe, and Win 7 then tells me it has failed to load the .dll. When I try to run the .exe on a XP SP2 machine, XP tells me that the file cannot be loaded.<\/p>\n<p>Here is the corresponding code:<\/p>\n<p>PySvc.py (This is the actual service, PySvc.py install is the string used to install it from the prompt :<\/p>\n<pre><code>import win32service  \nimport win32serviceutil  \nimport win32event\nimport servicemanager\n\nclass PySvc(win32serviceutil.ServiceFramework):  \n    _svc_name_ = \"PySvc\"  \n\n    _svc_display_name_ = \"Python Test\"  \n    _svc_description_ = \"Service Test\"  \n\n    def __init__(self, args):  \n        win32serviceutil.ServiceFramework.__init__(self,args)  \n        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  \n\n    # Meat and potatos. This is the actual code that's run. Currently testing\n    # to see behavior inside and outside of loop.\n    def SvcDoRun(self):  \n\n\n        f = open('test.dat', 'w+')  \n        rc = None  \n        f.write('OUTSIDE L00P\\n')  \n        # continue iteration if stop event not recieved  \n        while rc != win32event.WAIT_OBJECT_0:  \n            f.write('BEAUTIFUL TEST DATA NEW INSIDE L00P\\n')  \n            f.flush()  \n            # block for 5 seconds and listen for a stop event  \n            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)  \n\n        f.write('SHUTTING DOWN\\n')  \n        f.close()  \n\n    # called on shut down      \n    def SvcStop(self):  \n        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  \n        win32event.SetEvent(self.hWaitStop)  \n\nif __name__ == '__main__':  \n    win32serviceutil.HandleCommandLine(PySvc) \n<\/code><\/pre>\n<p>The following is the code for &#8220;setup.py&#8221;. Setup.py py2exe is the string used to create the .exe.<\/p>\n<pre><code>from distutils.core import setup\nimport py2exe\n\n\n\n\n\nsetup(\n            name = 'PySvc',\n            description = 'Service Test',\n            version = '1.00.00',\n                service = ['PySvc'],\n\n                console = ['PySvc.py'],\n                zipfile=None,\n                options = {\n                                \"py2exe\":{\n                                           \"includes\":\"win32service,win32serviceutil,win32event,servicemanager\",\n\n\n                                    },\n                            },\n    )                            \n<\/code><\/pre>\n<p>Due to the .exe&#8217;s apparent success on the local machine but failure on several other machines (that do not have python installed), I am currently inclined to believe that this is a problem with imports or the way that the imports are being compiled. I would be eternally grateful if anyone could provide some insight as to why this .exe doesn&#8217;t want to behave as it should. Thank you for your time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My goal is to create a single (or mutliple) .exe that can be used to install a Windows system service. Currently this is just a proof of concept so all my service does is write to a file to confirm it&#8217;s existence. When I invoke the service from a .py file it installs and runs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3704","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3704"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3704\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}