Using PythonService.exe to host python service while using virtualenv

前端 未结 4 1002
太阳男子
太阳男子 2020-12-05 20:28

I\'ve got a Windows 7 environment where I need to develop a Python Windows Service using Python 3.4. I\'m using pywin32\'s win32service module to setup the service and most

4条回答
  •  -上瘾入骨i
    2020-12-05 20:49

    For anyone reading in 2018, I didn't have any luck with either solution above (Win10, Python 3.6) - so this is what I did to get it working. The working directory is in site-packages/win32 on launch, so you need to change the working directory and fix the sys.path before you try and import any project code. This assumed venv sits in your project dir, otherwise you may just need to hard code some paths:

    import sys
    import os
    if sys.executable.lower().endswith("pythonservice.exe"):
        for i in range(4): # goes up 4 directories to project folder
            os.chdir("..")        
        # insert site-packages 2nd in path (behind project folder)
        sys.path.insert(1, os.path.join("venv",'Lib','site-packages'))
    
    [REST OF IMPORTS]
    class TestService(win32serviceutil.ServiceFramework):
        [...]
    

提交回复
热议问题