[Python-Dev] Inconsistent behaviour in import/zipimport hooks

Ulrich Berning ulrich.berning at desys.de
Mon Nov 14 16:53:30 CET 2005

Mark Hammond schrieb:

>>release. The main reason why I changed the import behavior was
>>pythonservice.exe from the win32 extensions. pythonservice.exe imports
>>the module that contains the service class, but because
>>pythonservice.exe doesn't run in optimized mode, it will only import a
>>.py or a .pyc file, not a .pyo file. Because we always generate bytecode
>>with -OO at distribution time, we either had to change the behavior of
>>pythonservice.exe or change the import behavior of Python.
>While ignoring the question of how Python should in the future handle
>optimizations, I think it safe to state that that pythonservice.exe should
>have the same basic functionality and operation in this regard as python.exe
>does.  It doesn't sound too difficult to modify pythonservice to accept -O
>flags, and to modify the service installation process to allow this flag to
>be specified.  I'd certainly welcome any such patches.
>Although getting off-topic for this list, note that for recent pywin32
>releases, it is possible to host a service using python.exe directly, and
>this is the technique py2exe uses to host service executables.  It would
>take a little more work to set things up to work like that, but that's
>probably not too unreasonable for a custom application with specialized
>distribution requirements.  Using python.exe obviously means you get full
>access to the  command-line facilities it provides.
Although off-topic for this list, I should give a reply.

I have done both.
My first approach was to change pythonservice.exe to accept -O and -OO 
and set the Py_OptimizeFlag accordingly.
Today, we aren't using pythonservice.exe any longer. I have done nearly 
all the required changes in win32serviceutil.py to let python.exe host 
the services. It requires no changes to the services, everything should 
work as before. The difference is, that the service module is always 
executed as a script now. This requires an additional (first) argument 
'--as-service' when the script runs as a service.

NOTE: Debugging services doesn't work yet.

Installing the service C:\svc\testService.py is done the usual way:
C:\svc>C:\Python23\python.exe testService.py install

The resulting ImagePath value in the registry is then:
"C:\Python23\python.exe" C:\svc\testService.py --as-service

After finishing development and testing, we convert the script into an 
executable with our own tool sib.py:
C:\svc>C:\Python23\python.exe C:\Python23\sib.py -n testService -d . 

Now, we just do:
C:\svc>testService.exe update

The resulting ImagePath value in the registry is then changed to:
"C:\testService.exe" --as-service

Starting, stopping and removing works as usual:
C:\svc>testService.exe start
C:\svc>testService.exe stop
C:\svc>testService.exe remove

Because not everything works as before (debugging doesn't work, but we 
do not use it), I haven't provided a patch yet. As soon as I have 
completed it, I will have a patch available.


More information about the Python-Dev mailing list