How can I change the Environmental PATH variable through code in python ? I am trying to return the path of an executable file. The code does not work because the shell is pointing to another directory. Any help is appreciated.
-
You should explain what your purpose is. Are you trying to get the path of an executable within the Python install directory, or an executable somewhere else on your system? The answer to those questions will be different.Brandon– Brandon2011-09-26 16:28:05 +00:00Commented Sep 26, 2011 at 16:28
-
I want to get path of an executable file in my system. But the shell is pointing to usr/bin .. my file is in usr/local so i am trying to point my PATH generally to usr/ directoryAmritha– Amritha2011-09-26 16:33:24 +00:00Commented Sep 26, 2011 at 16:33
Add a comment
|
3 Answers
You can use os.environ.
Example:
path = os.environ["PATH"] # a ':'-separated string
path += ":/var/custom/bin"
os.environ["PATH"] = path
Or in a single line:
os.environ["PATH"] = ':'.join(os.environ["PATH"].split(":") + ["/var/bin"])
Comments
You aren't looking for the PATH variable. You want to either set the current working directory with os.chdir or pass the absolute path with os.path.abspath.
Comments
os.environ["PATH"] += ":/usr/local/bin"