Help setting path on windows

Matt Gerrans mgerrans at ix.netcom.com
Wed Dec 5 10:04:51 CET 2001


I can think of at least three methods:

1) HKEY_LOCAL_MACHINE\Software\Python\PythonCore\2.1\PythonPath in Regedit and
add your path to the default path string there.
2) Add it to the PythonPath environment variable in autoexec.bat
3) Import sys, then do a sys.path.append('yourpath') in your script.
4) (oops -- IndexError: list index out of range!) Write a Python script that
uses win32api to do item one for you as needed; something like this should do
the trick:

from sys      import argv
from string   import lower
from string   import find
from win32api import RegOpenKeyEx
from win32api import RegQueryValueEx
from win32api import RegSetValueEx
from win32api import RegCloseKey
from win32con import HKEY_LOCAL_MACHINE
from win32con import KEY_ALL_ACCESS
from win32con import REG_SZ

def addToPyPath( newpart ):
 # Maybe get the version somewhere or search for it, or sort the
 # entries and use the most recent one, or specify which version's
 # path to modify, or finally: don't have several versions of Python
 # installed, like I have.
 keyname = r"SOFTWARE\Python\PythonCore\2.1\PythonPath"
 itemname = ""

 hkey = RegOpenKeyEx( HKEY_LOCAL_MACHINE, keyname, 0, KEY_ALL_ACCESS )
 pypath = RegQueryValueEx( hkey, itemname )[0]

 # If it is not already in the pythonpath, add it!
 if (find( lower(pypath), lower(newpart) ) < 0):
    RegSetValueEx( hkey, itemname, 0, REG_SZ, pypath + ';' + newpart )

 RegCloseKey(hkey)

if __name__ == '__main__':
   if len(argv) > 1:
      addToPyPath( argv[1] )
   else:
      print 'Please specify the part you want added to the Python path.'


- mfg

Stephen Boulet <stephen.boulet at motorola.com> wrote in message
news:3C0D1802.6D93D957 at motorola.com...
> Can someone help me?
>
> I'd like to be able to do:
>
>   from someModule import *
>
> and have my someModule.py file imported. Thanks.
>
> -- Stephen





More information about the Python-list mailing list