If you install python under windows and then open a command shell (DOS-prompt, you normally get an error message if you type "python" at the prompt. This is because the directory of the python executable is not in the PATH environment variable. If you know where you installed python, you can add this via Control Panel -> System -> Advanced -> Environment Variables but this is not very user friendly way of doing things and error prone.
This program, if run by double clicking the file or by dragging the file to a command shell, will add the directory of the executable associated with the .py extension to the PATH env. var (if it is not already in there). It will notify other programs of this change, but unfortunately command.com is not smart enough to understand that. You have to open a new command shell after running the program in order to be able to run "python" at the dos prompt.
If run with the optional command line parameter 'remove' the directory will be removed from the PATH.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | """
a small program to run after the installation of python on windows
adds the directory path to the python executable to the PATH env. variable
with optional parameter remove, removes it
you have to open a new command prompt to see the effects (echo %PATH%)
"""
import sys
import os
import time
import _winreg
import ctypes
def extend(pypath):
'''
extend(pypath) adds pypath to the PATH env. variable as defined in the
registry, and then notifies applications (e.g. the desktop) of this change.
Already opened DOS-Command prompt are not updated. Newly opened will have the
new path (inherited from the updated windows explorer desktop)
'''
hKey = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE,
r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
0, _winreg.KEY_READ | _winreg.KEY_SET_VALUE)
value, typ = _winreg.QueryValueEx (hKey, "PATH")
vals = value.split(';')
assert isinstance(vals, list)
if len(sys.argv) > 1 and sys.argv[1] == 'remove':
try:
vals.remove(pypath)
except ValueError:
print 'path element', pypath, 'not found'
return
print 'removing from PATH:', pypath
else:
if pypath in vals:
print 'path element', pypath, 'already in PATH'
return
vals.append(pypath)
print 'adding to PATH:', pypath
_winreg.SetValueEx(hKey, "PATH", 0, typ, ';'.join(vals) )
_winreg.FlushKey(hKey)
# notify other programs
SendMessage = ctypes.windll.user32.SendMessageW
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment')
def find_python():
'''
retrieves the commandline for .py extensions from the registry
'''
hKey = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
r'Python.File\shell\open\command')
# get the default value
value, typ = _winreg.QueryValueEx (hKey, None)
program = value.split('"')[1]
if not program.lower().endswith(r'\python.exe'):
return None
return os.path.dirname(program)
pypath=find_python()
extend(pypath)
|