Python: Platform independent way to modify PATH environment variable

resi picture resi · Nov 5, 2009 · Viewed 69.7k times · Source

Is there a way to modify the PATH environment variable in a platform independent way using python?

Something similar to os.path.join()?

Answer

RedGlyph picture RedGlyph · Nov 5, 2009

You should be able to modify os.environ.

Since os.pathsep is the character to separate different paths, you should use this to append each new path:

os.environ["PATH"] += os.pathsep + path

or, if there are several paths to add in a list:

os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)

As you mentioned, os.path.join can also be used for each individual path you have to append in the case you have to construct them from separate parts.