A lightweight Python library to persistently add or remove directories from PATH.
Project description
pathreg
Persistently add or remove directories from PATH on Windows, Linux, and macOS.
Installation
pip install pathreg
CLI Usage
pathreg add /some/directory # append (default)
pathreg add --index 0 /some/directory # insert at position 0
pathreg prepend /some/directory # insert at front
pathreg remove /some/directory
pathreg move /some/directory 0 # move existing entry to position 0
pathreg set /a /b /c # replace PATH with given directories
pathreg list
pathreg list --filter exists
pathreg list --filter writable
pathreg list --filter readable
pathreg list --filter is_symlink
pathreg list --filter is_real
pathreg list --filter is_empty
pathreg list --filter is_nonempty
pathreg list --filter has_executables
pathreg list --filter is_user
pathreg list --filter is_system
pathreg list --filter is_venv
pathreg list --filter has_executable --filter-arg python
pathreg list --filter depth --filter-arg 3
pathreg list --filter min_depth --filter-arg 2
pathreg list --filter max_depth --filter-arg 4
pathreg list --filter newer_than --filter-arg 7 # days
pathreg list --filter older_than --filter-arg 30
pathreg list --filter contains --filter-arg usr
pathreg list --filter matches --filter-arg "^/usr"
pathreg list --filter startswith --filter-arg /usr
pathreg count # prints number of PATH entries
pathreg check /some/directory # prints "yes" or "no"
pathreg find python # prints full path or "not found"
pathreg clean # removes duplicates and non-existent entries
Python API
from pathreg import add_path, prepend_path, remove_path, move_path, set_path, list_paths, path_len, in_path, find_executable, clean_path
from pathreg import filters
add_path("/some/directory") # append (default); idempotent
add_path("/some/directory", index=0) # insert at position 0; idempotent
prepend_path("/some/directory") # insert at front; idempotent
remove_path("/some/directory") # no-op if not found
move_path("/some/directory", 0) # move existing entry to position 0; no-op if absent
set_path(["/a", "/b", "/c"]) # replace PATH entirely
list_paths() # returns list[Path] of current PATH entries
list_paths(filters.exists) # pass any callable(Path) -> bool as filter
list_paths(filters.contains("usr")) # factory filters return a predicate
path_len() # returns number of PATH entries (faster than len(list_paths()))
in_path("/some/directory") # returns True if directory is in PATH
find_executable("python") # returns Path to first match, or None
clean_path() # removes duplicates and non-existent dirs, returns cleaned list[Path]
add_path, remove_path, move_path, and set_path modify the shell profile and the current process's PATH immediately (move_path only updates the current process).
Behavior
- Paths are normalized: trailing separators stripped, slashes converted per platform.
add_pathis idempotent, does nothing if the entry already exists. The optionalindexparameter controls insertion position (defaultNone= append to end); supports negative indices.move_pathmoves an existing entry to the given index in the current process PATH only; no-op if absent.remove_pathis a no-op if the entry is absent or the profile file does not exist.list_pathsreflects the current processPATH; it does not read the profile file. An optionalfilterpredicate (Callable[[Path], bool]) narrows the results.pathreg.filtersprovides ready-made predicates:- Plain:
exists,writable,readable,is_symlink,is_real,is_empty,is_nonempty,has_executables,is_user,is_system,is_venv - Factories (return a predicate):
contains(s),matches(pattern),startswith(prefix),has_executable(name),depth(n),min_depth(n),max_depth(n),newer_than(days),older_than(days) - Combinators:
not_(pred),all_(*preds),any_(*preds)
- Plain:
in_pathnormalizes trailing separators before comparing, matchingadd_pathbehaviour.find_executablewalks PATH entries in order and returns the first regular file that is executable, orNone.clean_pathremoves non-existent directories and duplicates (resolved via symlinks) from the current process PATH in-place, and returns the cleaned list.
Platform support
| Platform | Persistence target |
|---|---|
| Windows | HKCU\Environment via winreg; broadcasts WM_SETTINGCHANGE to notify the shell |
| bash | ~/.bash_profile (falls back to ~/.profile if it exists) |
| zsh | ~/.zshenv |
| sh | ~/.profile |
The active shell is detected from the SHELL environment variable.
No third-party dependencies required.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pathreg-1.0.7.tar.gz.
File metadata
- Download URL: pathreg-1.0.7.tar.gz
- Upload date:
- Size: 21.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d5db06bd85e22e930c3e4b5f4221322f33c8c681a95379c115ba9bc03a55670
|
|
| MD5 |
240c7171426038f715d06269207e14ab
|
|
| BLAKE2b-256 |
55cac4dd91e01f381b15166a8435aaa40348c24a350b9ebc492f1215bd9d62c8
|
Provenance
The following attestation bundles were made for pathreg-1.0.7.tar.gz:
Publisher:
publish.yml on tn3w/pathreg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pathreg-1.0.7.tar.gz -
Subject digest:
3d5db06bd85e22e930c3e4b5f4221322f33c8c681a95379c115ba9bc03a55670 - Sigstore transparency entry: 1245345808
- Sigstore integration time:
-
Permalink:
tn3w/pathreg@74fe13b1342c986c306dae517c1107df035f9946 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/tn3w
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@74fe13b1342c986c306dae517c1107df035f9946 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pathreg-1.0.7-py3-none-any.whl.
File metadata
- Download URL: pathreg-1.0.7-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41df0cb1100a7d5d79b19f276a2f3dae1df730110a6fb094c09f395e2bd4d639
|
|
| MD5 |
b50891194f46cafc3997b5e2d18e0136
|
|
| BLAKE2b-256 |
f8a82ce8d68b5bc12bed6688557ad45abb2ba452dbe743cb6eba0d92ac979de5
|
Provenance
The following attestation bundles were made for pathreg-1.0.7-py3-none-any.whl:
Publisher:
publish.yml on tn3w/pathreg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pathreg-1.0.7-py3-none-any.whl -
Subject digest:
41df0cb1100a7d5d79b19f276a2f3dae1df730110a6fb094c09f395e2bd4d639 - Sigstore transparency entry: 1245345810
- Sigstore integration time:
-
Permalink:
tn3w/pathreg@74fe13b1342c986c306dae517c1107df035f9946 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/tn3w
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@74fe13b1342c986c306dae517c1107df035f9946 -
Trigger Event:
push
-
Statement type: