Dave Angel davea at
Fri May 29 15:24:27 CEST 2009

Sergey Dikovitsky 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. 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 have to guess much of what you're talking about.  So if this is way 
off, try responding with a clearer wording.

PYTHONPATH is an environment variable.  You change that using your 
operating system's scripting language (shell script).

But you're talking about changing the first entry in sys.path, a Python 
list, which is generated from PYTHONPATH and a few other sources.  So it 
must be changed as Python is starting, not in the shell.

It is an ordinary list, and may be changed in the usual way.  So a 
script could start with:
   import sys
   sys.path = ["newpath"] + sys.path

(although you'd probably want to use a variable, not a literal)

But you say "without modifying of source code."  Not clear what that 
means, without you qualifying just which source code is sacrosanct.

It seems you're trying to change the search order for the initial 
script.  But there is no search order.  You give an actual filename to 
the interpreter, and it does not search at all.  So perhaps what you 
really want is to modify the PATH variable before running the script.  
Then the operating system will search for it before trying to execute 
it.  This should work as long as you don't need any other switches on 
the python.exe command line.

You need to specify your system environment (Windows, Linux, ...), and 
probably your Python version.  And make it clearer what your real goal is.

More information about the Python-list mailing list