Running a file, with a relative filename
Fredrik Lundh
fredrik at pythonware.com
Sat May 15 11:30:21 EDT 1999
Nathan Clegg <nathan at islanddata.com> wrote:
> > sys.path.insert(0, "../yyy")
> > import prog2
>
> I don't believe this will work unless you happen to be in the same
> directory as the toplevel script.
well, the question was if there was a way
to say:
import "../yyy/prog2.py"'
which is exactly what my snippet does. reading
Phil's post again, he might have meant "relative to
this module's location", in which case he could
add os.path.join(__file__, "../yyy") to the path
instead.
note that __file__ is only set for imported modules,
not for scripts. for scripts, you have to use some-
thing like os.path.dirname(sys.argv[0])...
> A more bulletproof method would append
> 'yyy' in some way to the current sys.path[0]
> entry.
you mean sys.argv[0], don't you? sys.path[0] tends
to be blank most of the time, at least on my boxes.
</F>
More information about the Python-list
mailing list