How do I permanently add the path to system's environment variable "PATH"?
I want to only add the path if it does not already exist.
Also I want to remove all paths that contain a folder name such as \myprogram
whether it be:
C:\path\to\myprogram\dist;
or D:\another\path\to\myprogram\dist;
I am inferring from the paths in your question that your are interested in doing this on the Windows platform.
The documentation describes the process:
There is a python package
pathtub
(I am the author), which can do the job. You can read the code and docs on GitHub.Installing
Example usage
Reading Path
Adding to path
Removing from Path
Requirements
Current implementation (v.1.1.2) uses Powershell for the permanent Path modifications. I have only tested this with newer (Python 3.7 and 3.8), but I guess it should work also on some older versions.
Here's something that does what you want which is similar to code in the jaraco.windows project. And like it, only uses built-in Python modules—so doesn't require first downloading and installing the pywin32 extensions. Plus it's Python 2.6+ and 3.x compatible and supports Unicode environment variables and values (directory paths in this case).
Note that Windows administrator rights are required to change the permanent system-level environment variables.
Sample usage:
You should check os.environ. It's a dictionary that can be manipulated directly or via os.putenv:
Hence:
[update]
according to this answer you can make them persistent via windows registry