4

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:

  • 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:

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'

w8eight
  • 605
  • 1
  • 6
  • 21
  • Install 225 version – PySaad Nov 26 '19 at 09:41
  • Note that if you want to use pywin32 for "system wide" features, such as registering COM objects or implementing Windows Services, then you must run the following command from an elevated command prompt (source https://github.com/mhammond/pywin32 ) – nithin Nov 26 '19 at 09:43
  • @nithin I did that as well, it was one of the answers from https://stackoverflow.com/questions/38197879/importerror-no-module-named-win32service – w8eight Nov 26 '19 at 09:44
  • @PySaad I am still getting same error – w8eight Nov 26 '19 at 09:46
  • Try both pip install pywin32 and pip install pypiwin32 – nithin Nov 26 '19 at 09:48
  • @nithin pypiwin32 is deprecated – PySaad Nov 26 '19 at 09:49
  • @w8eight ah ok check this post if it helps https://stackoverflow.com/questions/49307303/installing-the-pypiwin32-module/49307408#49307408 – nithin Nov 26 '19 at 09:49
  • @nithin I have pywin32 ver 223 and pypiwin32 installed now, and i cannot even debug a service now :( – w8eight Nov 26 '19 at 10:07

3 Answers3

1

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

w8eight
  • 605
  • 1
  • 6
  • 21
1

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
0

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

Ahmed Zakaria
  • 51
  • 1
  • 1