473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

look further down sys.path for extension module

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.
--
Vincent Wehren
Jul 18 '05 #1
4 1667
Vincent Wehren wrote:

[...]
the Python interpreter needs to be inserted into sys.path at index.


This should say "[...] into sys.path at index 0.

--
Vincent Wehren
Jul 18 '05 #2
Vincent Wehren wrote:
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
Jul 18 '05 #3
Martin v. Löwis wrote:
Vincent Wehren wrote:
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.


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!

--
Vincent Wehren
Jul 18 '05 #4
vincent wehren <vi*****@visualtrans.de> wrote:
...
> 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...


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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stefan Seefeld | last post by:
hi there, I'v trouble debugging an extension module with gdb and I'm wondering whether anybody has suggestions about how to do this. The symptoms of the bug I'm looking for are that...
1
by: Torsten Mohr | last post by:
Hi, i write an extension module in C at the moment. I want to define some constants (integer mainly, but maybe also some strings). How do i do that best within this extension module in C? ...
8
by: Torsten Mohr | last post by:
Hi, i write an extension module in C at the moment. This module does some work on some own data types that consist of some values. The functions that can change the data are written in C. ...
6
by: chris | last post by:
This is my first attempt at undertaking a C extension module. I want to wrap an existing C library so I can call the functions from Python. There are only two functions I'm interested in calling. ...
0
by: Jeremy Moles | last post by:
Hey guys. I have an extension module written in C that abstracts and simplifies a lot of what we do here. I'm observing some strange behavior and wanted to know if anyone had any advice as to how I...
3
by: Simon Burton | last post by:
Hi, I'm having some trouble linking one extension module to another because the linker expects a "lib" prefix and my python modules cannot have this prefix. I found two ways of doing it on a...
0
by: Tamas Nepusz | last post by:
Dear Python experts, I have a strange problem - or more precisely, I'm not even sure if it's a problem or not. I'm developing a Python extension module in C which creates a new type with...
3
by: rimmer | last post by:
I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats. ---------...
2
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.