windows - Using PythonService.exe to host python service while using virtualenv -


i've got windows 7 environment need develop python windows service using python 3.4. i'm using pywin32's win32service module setup service , of hooks seem working ok.

the problem when attempt run service source code (using python service.py install followed python service.py start). uses pythonservice.exe host service.py - i'm using venv virtual environment , script can't find it's modules (error message discovered python service.py debug).

pywin32 installed in virtualenv , in looking @ source code of pythonservice.exe, dynamically links in python34.dll, imports service.py , invokes it.

how can pythonservice.exe use virtualenv when running service.py?

thanks posting question , solution. took different approach might useful. pretty difficult find working tips python services, let alone doing virtualenv. anyway...

steps

this using windows 7 x64, python 3.5.1 x64, pywin32-220 (or pypiwin32-219).

  • open administrator command prompt.
  • create virtualenv. c:\python35\python -m venv myvenv
  • activate virtualenv. call myvenv\scripts\activate.bat
  • install pywin32, either:
  • run post-install script python myvenv\scripts\pywin32_postinstall.py -install.
    • this script registers dll's in system, , copies them c:\windows\system32. dll's named pythoncom35.dll , pywintypes35.dll. virtual environments on same machine on same major python point release share these... it's minor tradeoff :)
  • copy myvenv\lib\site-packages\win32\pythonservice.exe myvenv\scripts\pythonservice.exe
    • on service class (whatever subclasses win32serviceutil.serviceframework), set class property _exe_path_ point relocated exe. become service binpath. example: _exe_path_ = os.path.join(*[os.environ['virtual_env'], 'scripts', 'pythonservice.exe']).

discussion

i think why works python looks upwards figure out libs folders , based on sets package import paths, similar accepted answer. when pythonservice.exe in original location, doesn't seem work smoothly.

it resolves dll linking problems (discoverable depends.exe http://www.dependencywalker.com/). without dll business sorted out, won't possible import *.pyd files venv\lib\site-packages\win32 modules in scripts. example it's needed allow import servicemanager; servicemanager.pyd not in package .py file, , has cool windows event log capabilities.

one of problems had accepted answer couldn't figure out how accurately pick on package.egg-link paths created when using setup.py develop. these .egg-link files include path package when it's not located in virtualenv under myvenv\lib\site-packages.

if went smoothly, should possible install, start , test example win32 service (from admin prompt in activated virtualenv):

python venv\lib\site-packages\win32\demos\service\pipetestservice.py install python venv\lib\site-packages\win32\demos\service\pipetestservice.py start python venv\lib\site-packages\win32\demos\service\pipetestserviceclient.py 

the service environment

another important note in service execute python code in separate environment 1 might run python myservice.py debug. example os.environ['virtual_env'] empty when running service. can handled either:


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -