creating my own module search path

Bruce Sass bsass at freenet.edmonton.ab.ca
Mon Feb 26 03:17:46 EST 2001


On Sun, 25 Feb 2001, Sean 'Shaleh' Perry wrote:

> so, like a good little coder I use functions for code reusability and ease of
> maintenance.  I have a directory structure like:
>
> ./
>   frontend/
>   lib/
>   ...
>
> I would like to be able to load files in lib as python modules.  I know how
> to add the directory to sys.path.  The problem is I have a module named
> 'util.py'.  Sure, I could rename it.  But I can not possibly know the name of
> every module on a user's system.  What I was trying to do was add the path
> just before lib to the search path and load it as 'lib.util'.  But this does not
> work.  Suggestions anyone?

try this...

	cd .../site-packages
	mkdir sean
	touch __init__.py
	cp ..././lib/util.py sean

restart the interpreter and (from an actual test)...

>>> from Bruce import util
imported util.py from .../lib/python1.5/Bruce
>>> del util
>>> import Bruce.util
>>> Bruce.util.__doc__
'Bruce/util.py docstring.'
>>>

Bruce/util.py looks like this:

---8<---
"""Bruce/util.py docstring."""

print "imported util.py from .../lib/python1.5/Bruce"
--->8---

I'm not sure how correct this is, but it should be on the right track.

Maybe also have a look at .../lib/pythonX.Y/site.py.


- Bruce






More information about the Python-list mailing list