[Tutor] finding path to resource files in a GUI application

Michael Lange klappnase at freenet.de
Thu Jun 9 10:17:30 CEST 2005


On Thu, 9 Jun 2005 08:52:59 +0200
Christian Meesters <meesters at uni-mainz.de> wrote:

> Hi
> 
> Currently I'm writing a GUI application with wxPython (on OS X, but I  
> guess the problem is the same, regardless of the UNIX derivative one is  
> using). When I start the main script where it is located the  
> application finds all resource files (non-Python files like images and  
> html files for html windows) without any problem. However, if a put a  
> link in /usr/local/bin and start the script using the link the  
> application cannot find those resource files - unless, of course, I  
> will use full absolute paths to point to those files. One brief example  
> to illustrate the problem:
> 
> The class Goals is in a file called Help.py, located in '/gui_lib/' as  
> seen from my main script.
> 
> class Goals(wx.Frame):
> 	def __init__(self,parent,frame,title,size,pos=wx.DefaultPosition):
> 		wx.Frame.__init__(self,parent, 
> -1,title,size,style=wx.DEFAULT_FRAME_STYLE)
> 		self.frame = frame
> 		self.cwd = os.getcwd()	
> 	
> 		self.html = HtmlWindow(self,-1)
> 		self.html.LoadPage(os.path.join(self.cwd,'gui_lib/Goals.html'))  
> #this, of course, won't work
> 		#if the main script is called from somewhere else and not the  
> directory where the main script
> 		#is located
> 
> Any ideas what I could use instead of os.getcwd to construct a relative  
> path which will work even if I port the script to an other machine?
> 
> Cheers
> Christian
> 

Hi Christian,

try

    self.cwd = os.path.abspath(sys.path[0])

sys.path[0] is the directory of your main program file, with os.path.abspath() you can
get rid of the "../../" stuff at the beginning of the path if the program is called from a link.

I hope this helps

Michael




More information about the Tutor mailing list