473,543 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1672
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*****@visual trans.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
1047
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 depending on some seemingly benign (C/C++) code, the process will seg fault somewhere in the garbage collector's cleanup at the end of the application....
1
1417
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? Do i supply them as RO attributes?
8
2751
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. The question came up if this is by itself thread safe, if some two or more threads try to change these data types, are the C functions by themselves...
6
1271
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. I did mess with Pyrex a bit and Swig, to no avail, so I turned to doing it by hand. Using the example in Programming Python, I did get the easier...
0
995
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 should start tracking this down. More specific suggestions are obviously appreciated, but I really don't have a lot of information to provide so...
3
1861
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 linux box (either symlink or create a dummy .so that links to extension module) but I can get neither of them work on OSX (let alone windows).
0
953
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 methods, mapping support and stuff like that :) Everything's working fine, but if I inherit a new class from this type in Python, I start getting strange...
3
1434
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. --------- extTest.c --------- #include <stdio.h> double *testArray(int nsamp) {
2
2265
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 import TestDLL # my C Extension Module a = Numeric.array(, 'O' ) print 'a =', a print 'type a =', type(a)
0
7412
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4899
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3394
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3394
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
648
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.