[Tutor] sys.path and the path order

Garry Willgoose garry.willgoose at newcastle.edu.au
Fri Apr 23 02:34:02 CEST 2010

My question is so simple I'm surprised I can't find an answer  
somewhere. I'm interested if I can rely on the order of the  
directories in the sys.path list. When I'm running a file from the  
comand line like

python tellusim.py

The string in entry sys.path[0] appears to be the full path to the  
location of the file I'm running in this case tellusim ... i.e. it  
looks like '/Volumes/scone2/codes/tellusim0006'. This is good because  
for my code I need to create a search path for modules that is  
relative to the location of this file irrespective of the location I'm  
in when I invoke the script file (i.e. I could be in /Volumes/scone2  
and invoke it by 'python codes/tellusim0006/tellusim.py').

The question is can I rely on entry [0] in sys.path always being the  
directory in which the original file resides (& across linux, OSX and  
Windows)? If not what is the reliable way of getting that information?

More information about the Tutor mailing list