107

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

Something similar to os.path.join()?

0 _
  • 10,524
  • 11
  • 77
  • 109
resi
  • 1,738
  • 2
  • 13
  • 14

4 Answers4

212

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.

RedGlyph
  • 11,309
  • 6
  • 37
  • 49
21

Please note that os.environ is not actually a dictionary. It's a special dictionary-like object which actually sets environment variables in the current process using setenv.

>>> os.environ.__class__
<class os._Environ at 0x100472050>
>>> import os
>>> os.environ["HELLO"] = "WORLD"
>>> os.getenv("HELLO")
'WORLD'

This means that PATH (and other environment variables) will be visible to C code run in the same process.

(Since comments can't contain formatting, I have to put this in an answer, but I feel like it's an important point to make. This is really a comment on the comment about there being no equivalent to 'export'.)

0 _
  • 10,524
  • 11
  • 77
  • 109
Glyph
  • 31,152
  • 11
  • 87
  • 129
7

The caveat to be aware of with modifying environment variables in Python, is that there is no equivalent of the "export" shell command. There is no way of injecting changes into the current process, only child processes.

Matt T
  • 607
  • 3
  • 10
  • +1: Wise to point that out, commands like `os.system`, `os.popen` or `subprocess.Popen` should then be used from the Python application to launch other processes. Otherwise it's pretty much pointless. – RedGlyph Nov 05 '09 at 15:29
  • 5
    Clarification of Glyph's statement: this is not true because changes *are* injected into the current process (which is the python process). OP likely meant there's no way of injecting changes into the parent process (which is typically a shell that the python script was executed from). – PonyEars Aug 03 '13 at 09:34
  • 5
    If that's what OP means, then the shell doesn't have this capability either; `export` causes a variable to be copied into the environment of all *child* processes, but has no effect on the parent process. – Kyle Strand Jun 23 '14 at 15:26
0

You could refresh it like this

os.environ["PATH"] = os.environ["PATH"]
  • Hi, thanks for the code snippet. It would be great if you could comment on how exactly it solved the OP's problem so we could all learn from it! – Simas Joneliunas Jan 09 '22 at 10:29
  • Some installers modify PATH, some don't. For those which do, if you need to have current, "fresh" env PATH status [at] runtime of your script, you can refresh it like this. Example: I was installing gh via script. This installer modifies path. I needed access to "fresh" env path [at] runtime. This solution was sufficient. In other case (not modified PATH), solution posted above is better: os.environ["PATH"] += os.pathsep + path. – Paweł Terebiński Jan 11 '22 at 14:56