By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

zlib + Windows 32 service problem (ImportError)

P: n/a
Sorry, I realized that the import zlib was not executed from my
(working) service.
So here is the question: why can't I use zlib from a win32 service? Is
there any way to make it working?
-------------
Python could not import the service's module
File "T:\Python\Projects\NamedConnector\Service.py" , line 17, in ?
from Processor import *
File "c:\Python\Projects\NamedConnector\Processor.p y", line 35, in ?
from mess import MessageSocket
File "T:\Python\Lib\mess\MessageSocket.py", line 31, in ?
import zlib
exceptions.ImportError: dynamic module does not define init function
(initzlib)
-------------


Aug 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Laszlo Zsolt Nagy" <ga*****@geochemsource.com> schrieb im Newsbeitrag
news:ma***************************************@pyt hon.org...
| Sorry, I realized that the import zlib was not executed from my
| (working) service.
| So here is the question: why can't I use zlib from a win32 service? Is
| there any way to make it working?
|
| >-------------
| >Python could not import the service's module
| > File "T:\Python\Projects\NamedConnector\Service.py" , line 17, in ?
| > from Processor import *
| > File "c:\Python\Projects\NamedConnector\Processor.p y", line 35, in ?
| > from mess import MessageSocket
| > File "T:\Python\Lib\mess\MessageSocket.py", line 31, in ?
| > import zlib
| >exceptions.ImportError: dynamic module does not define init function
| >(initzlib)
| >-------------
|
|
|
I had a similar problem where a zlib.dll that is *not a Python extension* is
in sys.path *before* zlib.pyd. Python will try to import this zlib.dll and
find the dll doesn't export a initzlib:
for more info see
http://mail.python.org/pipermail/pyt...ad.html#248107

HTH,

Vincent Wehren


Aug 15 '05 #2

P: n/a
vincent wehren wrote:
"Laszlo Zsolt Nagy" <ga*****@geochemsource.com> schrieb im Newsbeitrag
news:ma***************************************@py thon.org...
| Sorry, I realized that the import zlib was not executed from my
| (working) service.
| So here is the question: why can't I use zlib from a win32 service? Is
| there any way to make it working?
|
| >-------------
| >Python could not import the service's module
| > File "T:\Python\Projects\NamedConnector\Service.py" , line 17, in ?
| > from Processor import *
| > File "c:\Python\Projects\NamedConnector\Processor.p y", line 35, in ?
| > from mess import MessageSocket
| > File "T:\Python\Lib\mess\MessageSocket.py", line 31, in ?
| > import zlib
| >exceptions.ImportError: dynamic module does not define init function
| >(initzlib)
| >-------------
|
|
|
I had a similar problem where a zlib.dll that is *not a Python extension* is
in sys.path *before* zlib.pyd. Python will try to import this zlib.dll and
find the dll doesn't export a initzlib:
for more info see
http://mail.python.org/pipermail/pyt...ad.html#248107

Thanks. I set my system environment variable 'PATH' to this:

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;%Syste mRoot%\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

Aug 16 '05 #3

P: n/a

"Laszlo Zsolt Nagy" <ga*****@geochemsource.com> schrieb im Newsbeitrag
news:ma***************************************@pyt hon.org...
| vincent wehren wrote:
|
| >"Laszlo Zsolt Nagy" <ga*****@geochemsource.com> schrieb im Newsbeitrag
| >news:ma***************************************@py thon.org...
| >| Sorry, I realized that the import zlib was not executed from my
| >| (working) service.
| >| So here is the question: why can't I use zlib from a win32 service? Is
| >| there any way to make it working?
| >|
| >| >-------------
| >| >Python could not import the service's module
| >| > File "T:\Python\Projects\NamedConnector\Service.py" , line 17, in ?
| >| > from Processor import *
| >| > File "c:\Python\Projects\NamedConnector\Processor.p y", line 35, in ?
| >| > from mess import MessageSocket
| >| > File "T:\Python\Lib\mess\MessageSocket.py", line 31, in ?
| >| > import zlib
| >| >exceptions.ImportError: dynamic module does not define init function
| >| >(initzlib)
| >| >-------------
| >|
| >|
| >|
| >I had a similar problem where a zlib.dll that is *not a Python extension*
is
| >in sys.path *before* zlib.pyd. Python will try to import this zlib.dll
and
| >find the dll doesn't export a initzlib:
| >for more info see
|
http://mail.python.org/pipermail/pyt...ad.html#248107

| >
| >
| Thanks. I set my system environment variable 'PATH' to this:
|
|
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;%Syste mRoot%\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.
Regards,
--

Vincent Wehren
Aug 16 '05 #4

P: n/a
|
|
C:\Python24;C:\Python24\DLLs;c:\Python24\Lib\si te-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;%Syste mRoot%\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.
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)

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)

print s1 == s2

True

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?
How can I make this code portable?

By the way, you have been a great help. Thank you very much. I can now
continue working. :-)

Les
Aug 17 '05 #5

P: n/a
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;%Syste mRoot%\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
Aug 17 '05 #6

P: n/a
Why do you think str() is needed here?

Because I'm not sure if sys.path was overwritten or changed. Some bad
modules could overwrite sys.path with another list. I know I'm paranoid. :-)
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).

Okay, I did so. I wrote a service that prints out sys.path into a
logfile. Here is the result:

sys.path=['C:\\Python24\\lib\\site-packages\\win32', 'T:\\Python\\Lib',
'C:\\WINDOWS\\system32\\python24.zip', 'C:\\WINDOWS\\system32',
'C:\\Python24\\DLLs', 'C:\\Python24\\lib',
'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk',
'C:\\Python24\\lib\\site-packages\\win32', 'C:\\Python24',
'C:\\Python24\\lib\\site-packages',
'C:\\Python24\\lib\\site-packages\\PIL',
'C:\\Python24\\lib\\site-packages\\win32\\lib',
'C:\\Python24\\lib\\site-packages\\Pythonwin',
'C:\\Python24\\lib\\site-packages\\wx-2.6-msw-ansi',
'T:\\Python\\Projects\\NamedConnector']

The empty string is not on sys.path. This is very strange, because it is
different when I start the python interactively. The problem was caused
by "C:\WINDOWS\system32", not the empty string. I'm still not sure why
it is included in sys.path, and why '' is not there? I also checked the
Python documentation about sys.path, and read the thread mentioned
before but still sys.path is magical, and magic is not Pythonic. :-)

Anyway, I think I have found the most platform independent solution.
Here it is:
import _socket
import os
import sys
dyndir = os.path.split(_socket.__file__)[0] # This can be "/usr/local/lib/python2.4/lib-dynload" or "C:\Python24\DLLs" or whatever sys.path.append(dyndir)


In most cases, '_socket.pyd' will be the first module that can be
imported, and it will by in the dynaload directory for sure.

I feel this is still unclean code. Do you think that it would be nice to
add new features to the sys module?

sys.dlpath - could be the path to the lib-dynload or DLLs folder
sys.libpath - could be the path to the lib folder

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

This cannot be used in conjunction with a windows service, because its
output cannot be seen. :-(
Aug 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.