Re: PYTHONPATH var

2009-05-29 Thread Steven D'Aprano
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PYTHONPATH var

2009-05-29 Thread Dave Angel

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.


--
http://mail.python.org/mailman/listinfo/python-list