Amritha
Amritha

Reputation: 805

PATH variable in Python

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.

Upvotes: 2

Views: 777

Answers (3)

miku
miku

Reputation: 187994

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"])

Upvotes: 4

Daniel Brockman
Daniel Brockman

Reputation: 19270

os.environ["PATH"] += ":/usr/local/bin"

See http://docs.python.org/library/os.html#os.environ.

Upvotes: 1

orlp
orlp

Reputation: 117641

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.

Upvotes: 2

Related Questions