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:
- from pypi:
pip install pypiwin32
, - from http://www.lfd.uci.edu/~gohlke/pythonlibs/:
pip install path\to\pywin32.whl
- from pypi:
- 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 namedpythoncom35.dll
,pywintypes35.dll
. virtual environments on same machine on same major python point release share these... it's minor tradeoff :)
- this script registers dll's in system, , copies them
- 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'])
.
- on service class (whatever subclasses win32serviceutil.serviceframework), set class property
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:
- setting environment variables inside script, e.g.
- find current path starting sys.executable, described in accepted answer.
- use path locate config file.
- read config file , put them in environment
os.environ
.
- add registry keys service environment variables.
- see accessing environment variables windows services doing manually regedit.exe
- see reg add reg_multi_sz multi-line registry value doing command line.
Comments
Post a Comment