Search code examples
pythonwindowspathsys

How to add a folder to the Windows PATH with Python?


I want to add a folder to the Windows PATH environment variable with Python. I tried these three code snippets but none worked:

os.environ['PATH'] += ";C:\my\folder"

and

sys.path.insert(0, os.path.abspath('C:\my\folder'))

and

if sys.platform == 'win32':
    sep = ';'
else:
    sep = ':'

os.environ['PATH'] += sep + r'"C:\my\folder"'

Solution

  • You should use:

    os.environ['PATH'] += R";C:\my\folder"