zlib + Windows 32 service problem (ImportError)

L

Laszlo Zsolt Nagy

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?
 
V

vincent wehren

| 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.py", 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/python-list/2004-October/thread.html#248107

HTH,

Vincent Wehren
 
L

Laszlo Zsolt Nagy

vincent said:
| 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.py", 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/python-list/2004-October/thread.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;%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
 
V

vincent wehren

| vincent wehren wrote:
|
| >| >| 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.py", 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
|
| >
| >
| 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;%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.


Regards,
 
L

Laszlo Zsolt Nagy

|
|
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.
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.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
 
J

John Machin

Laszlo said:
|

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.
Add this:
print os.getcwd() # see below for why
Why do you think str() is needed here?

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
 
L

Laszlo Zsolt Nagy

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. :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top