本文介绍了ModuleNotFoundError: 没有名为 'win32serviceutil' 的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以安装为 Windows 服务的项目,但我无法完成它.

Venv 是为这个项目准备的,安装了 pywin32 包(版本 227).但是,当我尝试从控制台运行 python 文件时:

导入 win32serviceutil

我收到以下错误:

ModuleNotFoundError: 没有名为win32"的模块

我尝试过的事情:

  • 重新安装软件包,并使用 python -m pip install pywin32
  • 重新安装
  • 将导入方式更改为:

    from win32 import win32serviceutil从 win32.lib 导入 win32serviceutil将 win32.lib.win32serviceutil 导入为 win32serviceutil

  • 来自

    奇怪的是,我可以运行以下命令并安装 Windows 服务:

    python MyPythonFile.py 安装

    它不会返回任何错误.但是尝试使用命令启动服务:

    python MyPythonFile.py start

    返回:

    错误 1053:服务没有及时响应启动或控制请求"

    在调试模式 python MyPythonFile.py debug 返回:

    ModuleNotFoundError: 没有名为win32serviceutil"的模块

    解决方案

    来自该线程的解决方案有效:在使用 virtualenv 的同时使用 PythonService.exe 托管 Python 服务

    我用来解决它的代码:

    导入操作系统导入系统service_directory = os.path.dirname(__file__)source_directory = os.path.abspath(service_directory)os.chdir(source_directory)venv_base = os.path.abspath(os.path.join(source_directory, "..", "..", "venv"))sys.path.append(".")old_os_path = os.environ['PATH']os.environ['PATH'] = os.path.join(venv_base, "Scripts")+ os.pathsep + old_os_pathsite_packages = os.path.join(venv_base, "Lib", "site-packages")prev_sys_path = list(sys.path)进口网站site.addsitedir(site_packages)sys.real_prefix = sys.prefixsys.prefix = venv_basenew_sys_path = list()对于列表中的项目(sys.path):如果项目不在 prev_sys_path 中:new_sys_path.append(item)sys.path.remove(item)sys.path[:0] = new_sys_path

    此代码必须在错误导入之前运行

    I have a project which can be installed as Windows Service, but i have troubles getting it done.

    Venv is prepared for this project with pywin32 package installed (version 227). However while I am trying to run a python file from console with:

    import win32serviceutil
    

    I am getting a following error:

    Things I tried:

    • reinstallation of package, and reinstallation with python -m pip install pywin32
    • changing import fashion to:

      from win32 import win32serviceutil from win32.lib import win32serviceutil import win32.lib.win32serviceutil as win32serviceutil

    • Answers from this thread

    win32 is recognized as folder by PyCharm:

    What is weird, I can run following command and install a Windows Service:

    python MyPythonFile.py install
    

    It does not return any errors. However trying to start the service with command:

    python MyPythonFile.py start
    

    returns:

    In debug mode python MyPythonFile.py debug it returns:

    解决方案

    The solution from this thread worked:Using PythonService.exe to host python service while using virtualenv

    Code which I used to resolve it:

    import os
    import sys
    
    service_directory = os.path.dirname(__file__)
    source_directory = os.path.abspath(service_directory)
    os.chdir(source_directory)
    venv_base = os.path.abspath(os.path.join(source_directory, "..", "..", "venv"))
    sys.path.append(".")
    old_os_path = os.environ['PATH']
    os.environ['PATH'] = os.path.join(venv_base, "Scripts")+ os.pathsep + old_os_path
    site_packages = os.path.join(venv_base, "Lib", "site-packages")
    prev_sys_path = list(sys.path)
    import site
    site.addsitedir(site_packages)
    sys.real_prefix = sys.prefix
    sys.prefix = venv_base
    new_sys_path = list()
    for item in list(sys.path):
        if item not in prev_sys_path:
            new_sys_path.append(item)
            sys.path.remove(item)
    sys.path[:0] = new_sys_path
    

    This code has to run before faulting imports

    这篇关于ModuleNotFoundError: 没有名为 'win32serviceutil' 的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 02:50