zlib + Windows 32 service problem (ImportError)

John Machin sjmachin at lexicon.net
Wed Aug 17 07:51:22 EDT 2005


Laszlo Zsolt Nagy wrote:
> |
> 
>> | 
>> C:\Python24;C:\Python24\DLLs;c:\Python24\Lib\site-packages\win32;c:\oracle\product\10.1.0\db_1\bin;c:\oracle\product\10.1.0\db_1\jre\1.4.2\bin\client;c:\oracle\product\10.1.0\db_1\jre\1.4.2\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program 
>>
>> | Files\Common Files\GTK\2.0\bin
>> |
>> | Then I restarted my computer. It is still missing initzlib. :-(
>> | Please note that I can run the same program as an application, 
>> logged in
>> | as the same user.
>> |
>> |  Les
>>
>> Changing the Windows dll search path doesn't make any difference. It 
>> is sys.path (Python's search path) that's causing you the headache. 
>> Please see the mentioned thread for proposed solutions.
>>  
>>
> Great. I could make my service working with this snippet:
> 
> import sys
> sys.path.insert(0,r'C:\Python24\DLLs')
> 
> But this is very ugly. Primarily, I do not want to use absolute path 
> names in a this program. I want to use the same code on different 
> computers and operating systems, but this code is not portable. 

Try this (or something like it):
if sys.platform == "win32":
     sys.path.insert(0, sys.exec_prefix + r'\DLLs')


> Secondly, I do not understand why sys.path is different when I start 
> python interactively. I believe that sys.path should be the same when 
> starting the program as a service. The only difference between the 
> application and the service is that the 'main' program of the service 
> imports some additional modules. See them below.
> 
> iT:\Python\Projects\NamedConnector>python
> Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on 
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import sys
>  >>>
>  >>> s1 = str(sys.path)
Add this:
print os.getcwd() # see below for why
>  >>>
>  >>> import win32serviceutil, win32service
>  >>> import pywintypes, win32con, winerror
>  >>> from win32event import *
>  >>> from win32file import *
>  >>> from win32pipe import *
>  >>> from win32api import *
>  >>> from ntsecuritycon import *
>  >>>
>  >>>
>  >>> s2 = str(sys.path)

Why do you think str() is needed here?

>  >>>
>  >>> print s1 == s2
> True

Add in here:

print sys.path
print os.getcwd() # see below for why

> 
> I cannot distribute my service until I make it independent of the python 
> installation directory.
> 
> Why sys.path is different when starting the code as a windows service?

Possibly because sys.path can start with '' which is interpreted as the 
current directory. Perhaps when the code is started as a windows service 
[I know nothing about windows services], the current directory is set to 
%windir%\system32 (where lots of DLLs hang out), and if there is a 
zlib.dll there, it will get picked up first. Try printing the current 
directory (see above).

Setting the PYTHONVERBOSE environment variable may assist in showing 
where modules are being loaded from.

HTH,
John



More information about the Python-list mailing list