Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any method to append path to environment variable 'PATH' in python

Tags:

python

I want to append the path to exists environment variable PATH using python script.

I have tried to use os.environ['path'] = 'C:\foo\bin:%PATH%', but its deleting all the existing paths and creating 'C:\foo\bin:%PATH%' as new path value.

os.environ['path'] = 'C:\foo\bin:%PATH%'
like image 208
KALYAN Avatar asked Jun 17 '26 04:06

KALYAN


1 Answers

You should be able to modify os.environ.

Since os.pathsep is the character to separate different paths, you should use this to append each new path:

os.environ["PATH"] += os.pathsep + path

or, if there are several paths to add in a list:

os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)

As you mentioned, os.path.join can also be used for each individual path you have to append in the case you have to construct them from separate parts.

like image 171
moe assal Avatar answered Jun 18 '26 18:06

moe assal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!