Back to index

python3.2  3.2.2
Functions | Variables
win_add2path Namespace Reference

Functions

def modify
def main

Variables

 HKCU = winreg.HKEY_CURRENT_USER
string ENV = "Environment"
string PATH = "PATH"
string DEFAULT = "%PATH%"

Detailed Description

Add Python to the search path on Windows

This is a simple script to add Python to the Windows search path. It
modifies the current user (HKCU) tree of the registry.

Copyright (c) 2008 by Christian Heimes <christian@cheimes.de>
Licensed to PSF under a Contributor Agreement.

Function Documentation

Definition at line 45 of file win_add2path.py.

00045 
00046 def main():
00047     paths, envpath = modify()
00048     if len(paths) > 1:
00049         print("Path(s) added:")
00050         print('\n'.join(paths[1:]))
00051     else:
00052         print("No path was added")
00053     print("\nPATH is now:\n%s\n" % envpath)
00054     print("Expanded:")
00055     print(winreg.ExpandEnvironmentStrings(envpath))

Here is the call graph for this function:

Definition at line 20 of file win_add2path.py.

00020 
00021 def modify():
00022     pythonpath = os.path.dirname(os.path.normpath(sys.executable))
00023     scripts = os.path.join(pythonpath, "Scripts")
00024     appdata = os.environ["APPDATA"]
00025     if hasattr(site, "USER_SITE"):
00026         userpath = site.USER_SITE.replace(appdata, "%APPDATA%")
00027         userscripts = os.path.join(userpath, "Scripts")
00028     else:
00029         userscripts = None
00030 
00031     with winreg.CreateKey(HKCU, ENV) as key:
00032         try:
00033             envpath = winreg.QueryValueEx(key, PATH)[0]
00034         except WindowsError:
00035             envpath = DEFAULT
00036 
00037         paths = [envpath]
00038         for path in (pythonpath, scripts, userscripts):
00039             if path and path not in envpath and os.path.isdir(path):
00040                 paths.append(path)
00041 
00042         envpath = os.pathsep.join(paths)
00043         winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath)
00044         return paths, envpath

Here is the caller graph for this function:


Variable Documentation

Definition at line 18 of file win_add2path.py.

string win_add2path.ENV = "Environment"

Definition at line 16 of file win_add2path.py.

win_add2path.HKCU = winreg.HKEY_CURRENT_USER

Definition at line 15 of file win_add2path.py.

Definition at line 17 of file win_add2path.py.