PYTHONPATH

harrismh777 harrismh777 at charter.net
Sat Apr 16 00:16:59 EDT 2011


Algis Kabaila wrote:
> Is PYTHONPATH a system variable that sets the
> path for several sessions and if so, where in the system is it?
> Do I need to create one for setting python path for several
> sessions?

It can be, and there are lots of ways to accomplish what you want, some 
of which depends on the platform you are using. I will show one of the 
ways that I accomplish this for my linux sessions. This is based on a 
very common snippet of code usually found in the users  .profile  which 
modifies the users path in the even the user has a ~/bin directory--- 
looks like this:


# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
     PATH="$HOME/bin:$PATH"
fi


When this snippet finds a ~/bin directory in the users ~/ then (and only 
then) it modifies the users bash session path with the ~/bin folder at 
the head of the path. Well, you can use this same snippet on your system 
to modify the PYTHONPATH system variable so that a special folder in 
your ~/ directory tree is at or near the head of the sys.path--- looks 
like this:


# set PATH so it includes user's private Python if it exists
if [ -d "$HOME/Python" ] ; then
     export PYTHONPATH="$HOME/Python:$PYTHONPATH"
fi


You will notice that there is a tiny baby change in the second 
snippet... the export. If you want you IDLE launches from the Desktop to 
"see" the path set in  .profile  provide that with the export.

Of course this only works if the Python folder exists in the users ~/ 
directory tree (or elsewhere, if you code it that way).

By default the sys.path always shows the directory python was opened in, 
usually the users home directory. With  .profile  you can set the path 
any way you want... most useful for setting up special test directories 
ahead of the "real" code, or for setting up separate directories for 
versions--- one for Python26, Python27, and of course Python32.


(there are other ways of accomplishing the same thing, and of course, 
this one only really works with *nix systems--- windows is another mess 
entirely)

kind regards,
m harris







More information about the Python-list mailing list