Using the environment module

The examples here all refer to the environment module.

Dump the current process environment

1
2
3
from winsys import environment

environment.Process ().dump ()

Remove all Python installations from the PATH

Prior, say, to installing a new version Python, make sure that no existing Python installations remain on the PATH. PATH is made up of the system PATH env var plus the user PATH.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from winsys import registry, environment

def munge_path (env, python_paths):
  #env['PATH'] = 
  print ";".join (
    p for p in env['PATH'].split (";") if not any (
      p.lower ().startswith (py) for py in python_paths
    )
  )

py = registry.registry (r"hklm\software\python\pythoncore")
py_paths = set (version.InstallPath.get_value ("").rstrip ("\\").lower () for version in py)
py = registry.registry (r"hkcu\software\python\pythoncore")
py_paths.update (version.InstallPath.get_value ("").rstrip ("\\").lower () for version in py)

munge_path (environment.user (), py_paths)
munge_path (environment.system (), py_paths)

The Python installations are listed in the registry under the SoftwarePython key in HKLM and HKCU. That key has one subkey for each version installed and the subkey holds its installation directory in the default value of the InstallPath key.

We collect the unique list of installation directories and filter the user and system PATH env vars in turn by including only those paths which are not part of a Python installation.

We have to allow for case differences between the PATH and the installation directories, and for the fact that some of the install dirs have a trailing backslash while some don’t.

Note

For the purposes of not endangering your PATH, the critical line which actually updates the PATH is commented out and the would-be result is shown instead.

Table Of Contents

Previous topic

Using the dialogs module

Next topic

Using the event_logs module

This Page