look further down sys.path for extension module

V

Vincent Wehren

Hi,

Is it possible to have Python look further down sys.path for a C
extension module when an ImportError is raised because of the "dynamic
module not implementing init function"?

The thing is, I have a case where the cwd of an application that embedds
the Python interpreter needs to be inserted into sys.path at index. For
the windows version there is a zlib.dll in the app's directory, which -
naturally - gets "import precedence" over zlib.pyd from the DLLs
directory. For the time being, I place copy zlib.pyd into he app's
directory to solve the problem, but it would be nice if there was a way
to avoid having to do this.


Any thoughts much appreciated.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Vincent said:
Is it possible to have Python look further down sys.path for a C
extension module when an ImportError is raised because of the "dynamic
module not implementing init function"?

You could use the imp module to import zlib.pyd "manually". Then, when
an "import zlib" occurs, it looks into sys.modules, finds that zlib is
already there, and forgets about looking into sys.path.

Alternatively, you could rebuild zlib.pyd to be named pyzlib.pyd. You
need to rebuild the code because you also need to rename the entry
function, and the name of the module inside the module itself. You
can then do

import pyzlib as zlib

Regards,
Martin
 
V

vincent wehren

Martin said:
You could use the imp module to import zlib.pyd "manually". Then, when
an "import zlib" occurs, it looks into sys.modules, finds that zlib is
already there, and forgets about looking into sys.path.

This sounds like a viable option. We already have some special hooks to
accomodate for using pywin32 from a network install so this would be
something along those same lines.
> Alternatively, you could rebuild zlib.pyd to be named pyzlib.pyd. You
need to rebuild the code because you also need to rename the entry
function, and the name of the module inside the module itself. You
can then do

import pyzlib as zlib

Yes, quickly tried that, too. It still is registered in sys.modules as
"pyzlib" - don't known if I expected that...


Thanks for your input!
 
A

Alex Martelli

vincent wehren said:
Yes, quickly tried that, too. It still is registered in sys.modules as
"pyzlib" - don't known if I expected that...

The merely local renaming should not accidentally cause multiple
repetitions of the module's loading if somewhere else the module gets
simply imported with 'import pyzlib', of course. So, it's recoded in
sys.modules['pyzlib'] -- anything else would be astonishing.

Just add a further statement
sys.modules['zlib'] = zlib
right after this import, if you want future 'import zlib' statements to
get 'redirected' to use the already-imported pyzlib instead.


Alex
 

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
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top