Permanently Adding A Directory to Pythonpath

Bengt Richter bokr at accessone.com
Tue May 29 15:18:17 EDT 2001


On Mon, 28 May 2001 22:10:21 -0700, "Jason Cunliffe"
<jasonic at nomadicsltd.com> wrote:

>> > I would like to know how to add a directory to my Pythonpath
>permanently.
>> I
>>
>> Simplest may be to have, in your PYTHONHOME directory (for
>> example, on Windows, D:\Python21 if you have Python 2.1 and
>> have chosen to install it there, as it its default), a text
>> file with any name and extension .PTH, listing, one per line,
>> the path of directories you want to be added to sys.path.
>
>As best I can tell, there appear to be at least 4 ways on Win98 to add to
>the PythonPath permanently. In descending order towards clearest and sanest
>[imho] these are:
>
>
>4.  Messing directly with the Registry
>
>3.  Appending a line to autoexec.bat
>    SET PYTHONPATH=C:\a...;C:\...;C:\..
>
>2.  Using PythonWin 'Edit Python Path' feature
>
>1.  Adding a .PTH file as suggested above
>
>Perhaps it is just me, but it it seems this is one FAQ of Python on Win32
>which could be much more clearly addressed for newbies and everyone.
>
>The .PTH solution makes the most sense becuase it is legible and not
>confused with other information somewhere in Windows guts. It appears to be
>the only one which is portable. But unfortunately its often not well
>mentioned in intro texts.
>
>Python is mostly beautifully portable. So why doe standard distibutions not
>include a little module of generic friendly commands to manage PYTHONPATH
>the same way for all systems. This should handle temporary interactive needs
>as well as more durable ones by putting basic wrapper functions using the
>.PTH feature. It would help standardize moduel distribution even further.
>
>For example:
>
>import sys
>
>sys.path.addPermanent("new_directory_name")      # adds to to .PTH
>sys.path.deletePermanent("old_directory_name ")  # removes from .PTH
>sys.path.suspendPermanent("some_directory_name") # only hide for this
>session
>
>Not doubt there is nicer syntax you can think of..
>
>Am I missing something here?
>
Well, sys.path already has methods by its nature as a [] thingy,
so why not just use those? What you'd need is a module that
got imported by the main on startup.  Being a pythonewbie,
I'm not sure something like that doesn't already exist ;-)

E.g., what if starting the interpreter would automatically
import, e.g., config.py it it existed in the PYTHONHOME
directory? That would seem pretty extendable and flexible.

Maybe whatever is reading .PTH files is it? Is it defined
in a user-modifiable place?



More information about the Python-list mailing list