PYTHONPATH var

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri May 29 08:55:51 EDT 2009


On Fri, 29 May 2009 03:50:54 -0700, insfor wrote:

> Hi guys. I have a question regarding runtime definition of the variable
> PYTHONPATH. Do you know how without modifying of source code change the
> value for this var.

"Syntax error:  sentence seems to be a question, but is missing a 
question mark."

To answer your question, PYTHONPATH is an environment variable. You set 
it in your shell. For example, I use the bash shell under Linux, and in 
my .bashrc file I have this line:

export PYTHONPATH=/home/steve/python/

Every time I log in, the shell sets the environment variable to the 
pathname /home/steve/python/, and then when Python runs, it appends that 
path to sys.path. I don't have to modify any Python source code.



> Value stores in the system var sys.path, but the
> first item of this list, path[0], is the directory containing the script
> that was used to invoke the Python interpreter. We need to change this
> value which allows to us import scripts first from directory containing
> newest hotfix scripts without replacing original project scripts. One of
> the variant is to create script which will modify this var and insert
> line with import this script into each project script. So the question
> does another way exist? For example, parameter or argument for python
> launcher.

I don't understand what you are actually trying to say here. Perhaps you 
can explain a little bit more carefully?


However, trying to guess what you want, PYTHONPATH doesn't *replace* 
sys.path, it appends to the end of it. This is usually the right thing to 
do. However, sys.path is an ordinary list. If you want to modify it, you 
can do so:

import sys
sys.path[0] = '/some/other/path'



-- 
Steven



More information about the Python-list mailing list