[Tutor] sys.path and the path order

Steven D'Aprano steve at pearwood.info
Fri Apr 23 05:42:43 CEST 2010


On Fri, 23 Apr 2010 10:34:02 am Garry Willgoose wrote:
> My question is so simple I'm surprised I can't find an answer
> somewhere. 

Did you read the Fine Manual?


> I'm interested if I can rely on the order of the 
> directories in the sys.path list.
[...] 
> 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)? 


The documentation says:

sys.path

A list of strings that specifies the search path for modules. 
Initialized from the environment variable PYTHONPATH, plus an 
installation-dependent default.

As initialized upon program startup, the first item of this list, 
path[0], is the directory containing the script that was used to invoke 
the Python interpreter. If the script directory is not available (e.g. 
if the interpreter is invoked interactively or if the script is read 
from standard input), path[0] is the empty string, which directs Python 
to search modules in the current directory first. Notice that the 
script directory is inserted before the entries inserted as a result of 
PYTHONPATH.

http://docs.python.org/library/sys.html#sys.path

So the answer to your question is, Yes.


-- 
Steven D'Aprano


More information about the Tutor mailing list