无法调试pywin32服务

时间:2019-01-29 09:22:46

标签: python pywin32

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

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 在哪里搜索模块。

相关问题