473,385 Members | 1,620 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.

import hook

Hi - Is it possible to override the import process so that if in my program
I do

import foo.bar

Python will look for bar in a directory which isn't called foo?

I want my module/program to be able to be run without being installed in
site-packages, so by doing "import foo.bar", it should start looking for
bar in the current directory which could be called "foo-0.43".

I've tried overriding __import__, chopping out "foo." from package names,
but that tends to break. I've also tried overriding imp.find_module() but
Python never appears to use my version.

Any ideas?

Thanks

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Jun 11 '06 #1
4 1734
Jeremy Sanders wrote:
Hi - Is it possible to override the import process so that if in my program
I do (...)
Any ideas?

Why not handle the foo.bar/version string separately and just append
the resulting path to sys.path?

Jun 11 '06 #2
Jeremy Sanders <je*******************@jeremysanders.net> wrote:
Hi - Is it possible to override the import process so that if in my program
I do

import foo.bar

Python will look for bar in a directory which isn't called foo?

I want my module/program to be able to be run without being installed in
site-packages, so by doing "import foo.bar", it should start looking for
bar in the current directory which could be called "foo-0.43".

I've tried overriding __import__, chopping out "foo." from package names,
but that tends to break. I've also tried overriding imp.find_module() but
Python never appears to use my version.

Any ideas?


Yes, PEP 302 (which despite being marked as "draft" has in fact been
already mostly implemented, since it's used by the zipimport mechanism)
allows you to perform such feats. Study it at
<http://www.python.org/dev/peps/pep-0302/> ...
Alex
Jun 11 '06 #3
Alex Martelli wrote:
Jeremy Sanders <je*******************@jeremysanders.net> wrote:
Hi - Is it possible to override the import process so that if in my program
I do

import foo.bar

Python will look for bar in a directory which isn't called foo?

I want my module/program to be able to be run without being installed in
site-packages, so by doing "import foo.bar", it should start looking for
bar in the current directory which could be called "foo-0.43".

I've tried overriding __import__, chopping out "foo." from package names,
but that tends to break. I've also tried overriding imp.find_module() but
Python never appears to use my version.

Any ideas?


Yes, PEP 302 (which despite being marked as "draft" has in fact been
already mostly implemented, since it's used by the zipimport mechanism)
allows you to perform such feats. Study it at
<http://www.python.org/dev/peps/pep-0302/> ...


There are also other ways. You could extend __path__ of foo, and the
pkgutil module might also be useful.

Thomas

Jun 11 '06 #4
Thomas Heller wrote:
There are also other ways. You could extend __path__ of foo, and the
pkgutil module might also be useful.


The __path__ trick worked nicely, thanks. Here is the code in case anyone is
interested

# Allow veusz to be run even if not installed into PYTHONPATH
try:
import veusz
except ImportError:
# load in the veusz module, but change its path to
# the veusz directory, and insert it into sys.modules
import __init__ as veusz
thisdir = os.path.dirname( os.path.abspath(__file__) )
veusz.__path__ = [thisdir]
veusz.__name__ = 'veusz'
sys.modules['veusz'] = veusz

This is part of the main program. If it can't import it (i.e. it is not
installed), it imports the __init__ module, renames it, and corrects its
path, then sticks it into the list of imported modules.

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Jun 18 '06 #5

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

Similar topics

8
by: Grant D. Watson | last post by:
If this has been answered before, or if my terminology is off, please bear with me; my Python experience is limited to use in one class and to personal projects. I'd like to do something rather...
0
by: F. GEIGER | last post by:
Hi all, I use Leo for programming. Among other goodies Leo provides it let's me easily share code between different Python apps. When I have an app consisting of more than one file, they usually...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
0
by: Noam Raphael | last post by:
Hello, Do you know of a reference implementation of an import hook? I mean a class, written in Python, that imitates Python's default module loading behaviour. I mean that I would be able to...
0
by: Mustafa Thamer | last post by:
Hi, I'm using import hooks according to PEP 302, in order to load python files from a game PAK file. The game is C++ using embedded and extended Python (v2.33) and Boost. The importing works...
97
by: Kjetil Torgrim Homme | last post by:
often when re-factoring code, I need to change the indent level of some chunk of code. due to the lack of an end marker, my Emacs has to use heuristics when re-indenting, and this will...
2
by: Torsten Mohr | last post by:
Hi, is there some description available to overwrite the import hook? By googling i found out so far that i need to overwrite __builtins__.__import__ with something else. Can i also do this...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
79
by: pinkfloydhomer | last post by:
I want to scan a file byte for byte for occurences of the the four byte pattern 0x00000100. I've tried with this: # start import sys numChars = 0 startCode = 0 count = 0
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.