Safest manner to extend search path for modules?
Mike Meyer
mwm at mired.org
Wed Jul 27 00:26:00 EDT 2005
"Joseph Turian" <turian at gmail.com> writes:
> Hi,
>
> What is the safest manner to extend search path for modules, minimizing
> the likelihood of shooting oneself in the foot?
>
> The system (which includes scripts and their shared modules) may be
> checked out in several different locations, but a script in a
> particular checked-out version of the system should only use modules
> from that checkout location.
>
> e.g. if the system contains a directory scripts/foo/ where all the
> scripts are housed, and scripts/modules/ where all the modules are
> housed, then is it correct for each script in scripts/foo/ to begin
> with:
> import sys, os.path
> sys.path.append(os.path.join(sys.path[0], "../modules"))
>
> If so, is there a cleaner way of doing this than including the above
> text in all scripts?
Put that text in a module that is already on the path, and then import
that module.
Or mabye generalize it into a function:
import sys, os.path
def fixpath(*new):
sys.path.extend(os.path.join(sys.path[0], x) for x in new)
This requires 2.4. Change it to ([os.path ... new]) for earlier versions.
Of course, I note that sys.path[0] is '', so the join doesn't do anything.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list