w8eight
w8eight

Reputation: 623

ModuleNotFoundError: No module named 'win32serviceutil'

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:

ModuleNotFoundError: No module named 'win32'

Things I tried:

win32 is recognized as folder by PyCharm:

enter image description here

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:

"Error 1053: The service did not respond to the start or control request in a timely fashion"

In debug mode python MyPythonFile.py debug it returns:

ModuleNotFoundError: No module named 'win32serviceutil'

Upvotes: 4

Views: 19396

Answers (3)

Ugleiton Carneiro
Ugleiton Carneiro

Reputation: 11

for me it worked like this:

import os
import sys
import site

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)
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

Upvotes: 1

Ahmed Zakaria
Ahmed Zakaria

Reputation: 51

Good Work :)

To get this to work with me I had to change the path of the virtual env folder reflected in the following line to point to my venv folder

from this: venv_base = os.path.abspath(os.path.join(source_directory, "..", "..", "venv"))

To this: venv_base = os.path.abspath(os.path.join(source_directory, "..", "..", "venv"))

Upvotes: 0

w8eight
w8eight

Reputation: 623

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

Upvotes: 1

Related Questions