无法调试pywin32服务

Can't debug pywin32 service

我正在尝试使用 PythonService.exe 调试我的服务,但我收到一个奇怪的错误:

PS C:\Users\rs_al\Dev\PyXLSQL> py serviceapp.py install
Installing service pyxlsql
Changing service configuration
Service updated
PS C:\Users\rs_al\Dev\PyXLSQL> py serviceapp.py debug
Debugging service pyxlsql - press Ctrl+C to stop.
Error 0xC0000004 - Python could not import the service's module

ModuleNotFoundError: No module named 'w32service'

(null): (null)

项目结构

serviceapp.py
    w32service\
               __init__.py
               service.py

如果我将整个代码从 service.py 移动到 serviceapp.py,我可以毫无问题地调试它。

编辑:

gui.py
gui\
    __init__.py
    menu.py
    pageone.py
    pagetwo.py
    pagethree.py

它完美地作为 *.py.*exe

Python 解释器不知道去哪里寻找你的 w32service 模块(包)。一种方法是将其路径添加到 [Python 3.Docs]: Modules - The Module Search Path 导入它之前):

import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

from w32service.service import WinService

# ...

为了让事情更清楚,在导入任何东西之前使用 print(sys.path)(当然,sys 除外)以查看 Python 在哪里 搜索模块。