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.
asked Sep 26, 2011 at 16:17
Amritha
8153 gold badges10 silver badges26 bronze badges
-
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
3 Answers 3
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"])
answered Sep 26, 2011 at 16:19
miku
189k47 gold badges314 silver badges317 bronze badges
Sign up to request clarification or add additional context in comments.
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.
answered Sep 26, 2011 at 16:19
orlp
119k39 gold badges226 silver badges325 bronze badges
Comments
os.environ["PATH"] += ":/usr/local/bin"
answered Sep 26, 2011 at 16:21
Daniel Brockman
19.4k3 gold badges32 silver badges43 bronze badges
Comments
lang-py