You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.
frompathtubimportget_path# Reads os.environ['PATH'], which is same as PATH (cmd) or $Env:Path (powershell)path=get_path() # same as get_path('process')# Reads [Environment]::GetEnvironmentVariable('Path', 'User')path_user=get_path("user")
# Reads [Environment]::GetEnvironmentVariable('Path', 'Machine')path_machine=get_path("machine")
Example output
In [1]: print(get_path('user')) # returns a str
C:\Python\Python37\Scripts\;C:\Python\Python37\;C:\Python\Python37-32\Scripts\;C:\Python\Python37-32\;C:\Users\USER\AppData\Roaming\npm;C:\Users\USER\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Microsoft VS Code\bin;C:\Programs;C:\Programs\fciv;C:\texlive\2018\bin\win32;C:\Programs\apache-maven-3.6.2\bin;C:\Program Files\Java\jdk-13.0.1\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Programs\cloc;C:\Users\USER\AppData\Local\Programs\Microsoft VS Code\bin;
Adding permanently to PATH (User)
Setting PATH (User) variable will make permanent changes to PATH variable (of course the changes can be reverted by removing the item from the PATH.)
Adding a folder to PATH
In [1]: frompathtubimportadd_to_pathIn [2]: added=add_to_path(r'C:\My new folder\added to user PATH','user')
In [3]: addedOut[3]: True# There is protection against adding duplicate entriesIn [4]: added=add_to_path(r'C:\My new folder\added to user PATH', 'user')
In [5]: addedOut[5]: False
User PATH before and after running the code above.
Adding permanently to PATH (System/Machine)
Similar to setting User PATH variables
Setting PATH (System/Machine) variable will make permanent changes to PATH variable (of course the changes can be reverted by removing the item from the PATH.)
Change mode to "machine" and Run the script with Admin rights.
from pathtub import add_to_path
added = add_to_path(r'C:\My new folder\added to machine PATH', mode='machine')
Removing permanently from PATH (User)
In [1]: frompathtubimportremove_from_pathIn [2]: removed=remove_from_path(r'C:\My new folder\added to user PATH', 'user')
In [3]: removedOut[3]: True# Can only remove once. Safe to call multiple times.In [4]: removed=remove_from_path(r'C:\My new folder\added to user PATH', 'user')
In [5]: removedOut[5]: False
Removing permanently from PATH (System/Machine)
Similar to removing User PATH variables
Change mode to "machine" and Run the script with Admin rights.
from pathtub import add_to_path
removed = remove_from_path(r'C:\My new folder\added to machine PATH', mode='machine')
Checking if folder is in PATH
Note: You don't have to worry if the saved PATH item ends with a backslash or not; both cases are checked.
# Check the os.environ['PATH'] / PATH (cmd) / $Env:Path (Powershell) found=is_in_path() # same as is_in_path("process") # Checks the `[Environment]::GetEnvironmentVariable('Path','User')`; The "User PATH"found_user=is_in_path("user")
# Checks the `[Environment]::GetEnvironmentVariable('Path','Machine')`; The "System PATH"found=is_in_path("machine")
Example
In [1]: frompathtubimportis_in_pathIn [2]: found=is_in_path('C:\\Python\\Python37\\', 'user')
In [3]: foundOut[3]: True# It does not matter if the seach folder or the folder saved# to PATH has "\" as the last character.In [4]: found=is_in_path(r'C:\Python\Python37', 'user')
In [5]: foundOut[5]: TrueIn [6]: found=is_in_path(r'C:\Nonexistent\path', 'user')
In [7]: foundOut[7]: False