Running a file, with a relative filename

Phil Hunt philh at vision25.demon.co.uk
Sat May 15 12:08:00 EDT 1999


In article <015101be9eeb$5d7d52b0$f29b12c2 at pythonware.com>
           fredrik at pythonware.com "Fredrik Lundh" writes:

> 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])...

What's the difference? If I run (from the shell prompt):

   $ python prog1.py

Is prog1 a script or a module?

What if I run 
 
   $ prog1.py

and start the file with #!/usr/lib/python ?


-- 
Phil Hunt....philh at vision25.demon.co.uk





More information about the Python-list mailing list