sasi
sasi

Reputation: 41

how to set PATH=%PATH% as environment in Python script?

I'am trying to start an exe file (the exe file is the output of c++ project compiled with visual studio) from a python program. In the properties of this c++ project (configuration ->properties-> debugging-> environment) the following setting in the

     (PATH = %PATH%;lib\testfolder1;lib\testfolder2) 

is given.
is there any way to set path environment variable to

  1. PATH = %PATH%
  2. lib\testfolder1
  3. lib\testfolder2

in a python program?

Thanks in advance for your replay

Upvotes: 4

Views: 19099

Answers (3)

oldpride
oldpride

Reputation: 985

Repost Yaron's answer but dropped the sys.path. As in his post's comment, sys.path is for module search, not command search. PATH is for command search.

import os
os.environ["PATH"] += os.pathsep + os.pathsep.join(["c:\\new\\path"])
print os.environ["PATH"]

Upvotes: 2

Zachary Vorhies
Zachary Vorhies

Reputation: 11

Here is a solution that I created. It will check to see if the path already exists, and if not then it will write it to the registry for both the the current user and machine.

I've tested that the current process will not have the updated environment, but a new command line launched from the Run command will be updated.

Note that when a new window is created that it will have the updated path values.

def AppendWindowsPath(path):
  def AddPathInRegistry(HKEY, reg_path, new_path):
    reg = None
    key = None
    try:
      reg = ConnectRegistry(None, HKEY)
      key = OpenKey(reg, reg_path, 0, KEY_ALL_ACCESS)
      path_string, type_id = QueryValueEx(key, 'PATH')
      path_list = [f.strip("\r\n") for f in  path_string.split(';') if f]

      if new_path in path_list:
        print(new_path + " is already in %PATH%")
        return "ALREADY_IN_ENVIRONMENT"

      print("Adding " + new_path + " to %PATH%")
      SetValueEx(key, 'PATH', 0, REG_EXPAND_SZ, path_string + ";" + new_path)
      return "UPDATED_PATH"
    except Exception as e:
      print("ERROR while executing registry edit with " + str(HKEY) + "/" + reg_path)
      return "ERROR"
    finally:
      if key: CloseKey(key)
      if reg: CloseKey(reg)


  # Add the path to the current machine
  result_machine = \
    AddPathInRegistry(HKEY_LOCAL_MACHINE,
                      r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
                      path)

  # Update the path for the current user.
  result_user = \
    AddPathInRegistry(HKEY_CURRENT_USER, r'Environment', path)

  if ("UPDATED_PATH" in result_machine) or ("UPDATED_PATH" in result_user):
    # Updates Environment.
    win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')

Upvotes: 0

Yaron
Yaron

Reputation: 10450

You can update PATH using several methods:

import sys
sys.path += ["c:\\new\\path"]
print sys.path

or

import os
os.environ["PATH"] += os.pathsep + os.pathsep.join(["c:\\new\\path"])
print os.environ["PATH"]

Upvotes: 8

Related Questions