| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  | """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. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import site | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2008-05-25 07:45:51 +00:00
										 |  |  | import winreg | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-25 07:45:51 +00:00
										 |  |  | HKCU = winreg.HKEY_CURRENT_USER | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  | ENV = "Environment" | 
					
						
							|  |  |  | PATH = "PATH" | 
					
						
							| 
									
										
										
										
											2010-08-02 22:59:44 +00:00
										 |  |  | DEFAULT = "%PATH%" | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def modify(): | 
					
						
							|  |  |  |     pythonpath = os.path.dirname(os.path.normpath(sys.executable)) | 
					
						
							|  |  |  |     scripts = os.path.join(pythonpath, "Scripts") | 
					
						
							|  |  |  |     appdata = os.environ["APPDATA"] | 
					
						
							|  |  |  |     if hasattr(site, "USER_SITE"): | 
					
						
							|  |  |  |         userpath = site.USER_SITE.replace(appdata, "%APPDATA%") | 
					
						
							|  |  |  |         userscripts = os.path.join(userpath, "Scripts") | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         userscripts = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-25 07:45:51 +00:00
										 |  |  |     with winreg.CreateKey(HKCU, ENV) as key: | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2008-05-25 07:45:51 +00:00
										 |  |  |             envpath = winreg.QueryValueEx(key, PATH)[0] | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  |         except WindowsError: | 
					
						
							|  |  |  |             envpath = DEFAULT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         paths = [envpath] | 
					
						
							|  |  |  |         for path in (pythonpath, scripts, userscripts): | 
					
						
							|  |  |  |             if path and path not in envpath and os.path.isdir(path): | 
					
						
							|  |  |  |                 paths.append(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         envpath = os.pathsep.join(paths) | 
					
						
							| 
									
										
										
										
											2008-05-25 07:45:51 +00:00
										 |  |  |         winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath) | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  |         return paths, envpath | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     paths, envpath = modify() | 
					
						
							|  |  |  |     if len(paths) > 1: | 
					
						
							| 
									
										
										
										
											2009-11-09 00:49:19 +00:00
										 |  |  |         print("Path(s) added:") | 
					
						
							|  |  |  |         print('\n'.join(paths[1:])) | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2009-11-09 00:49:19 +00:00
										 |  |  |         print("No path was added") | 
					
						
							|  |  |  |     print("\nPATH is now:\n%s\n" % envpath) | 
					
						
							|  |  |  |     print("Expanded:") | 
					
						
							|  |  |  |     print(winreg.ExpandEnvironmentStrings(envpath)) | 
					
						
							| 
									
										
										
										
											2008-01-18 18:40:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |