ALAN GAULD wrote:

> That's a great idea Mr. Gauld!
> But I have no idea how to do that...
> can you maybe write a small example for a newb like me?? pretty please with 
cheese on top??
> > > base = pic(file="base.gif")
> > > > > > Now, I'm just wondering... for the "base.gif" part, would it be better
> > > to write out the whole directory?
>
> > No because that requires that your user install the program and
> > other files exactly where you did. Thats not friendly. Instead use
> > a relative path,
To use a relative path base = pic(file=root+"/base.gif")
<snip...>

I would get 'root' this way, rather than requiring anything to be set up by the install (registry, environment vars, config):

root = os.path.dirname(__file__)
  or  sometimes
root = os.path.dirname(mymodule.__file__) (assuming mymodule is a successfully imported module)

And then I'd turn the relative path into an absolute with:
    os.path.join(root, "base.gif")


I use this technique whenever I need to locate something relative to the "installed" code, in other words to fetch const data that was copied with the .py files.

I reserve using install variables to finding things relating to the choices the user made at install time, which may very well vary per user. And sometimes that means I can avoid install parameters entirely, letting the install be simply two steps: 1) unzip the files into an appropriate place (arbitray, and user chosen, but typically in a subdirectory of c:\program files on Windows). 2) Set up a batch file, shortcut, path string, or file association to point to the executable(s) in that directory.


(Does anybody think that using __file__ this way is uncool? For example, should I better use sys.path[0] ? Or something more convoluted involving sys.modules ?)

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to