import hook

J

Jeremy Sanders

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
 
R

Rune Strand

Jeremy said:
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?
 
A

Alex Martelli

Jeremy Sanders said:
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
 
T

Thomas Heller

Alex said:
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
 
J

Jeremy Sanders

Thomas said:
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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top