userpath

1.9.2 · active · verified Sun Mar 29

userpath is a cross-platform Python tool designed to simplify the modification of a user's PATH environment variable without requiring elevated privileges. It provides a programmatic and command-line interface to append, prepend, or verify locations within the user's PATH. The library is actively maintained, with its latest version being 1.9.2, and has a consistent release cadence addressing bug fixes and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to check for, add, and verify a directory in the user's PATH. It also informs the user if a shell restart is necessary. The example uses a placeholder directory in the user's home directory.

import userpath
import os

# Define a hypothetical directory to add to PATH
location_to_add = os.path.expanduser('~/.local/bin/my_custom_tool')

print(f"Checking if '{location_to_add}' is in current shell's PATH...")
if userpath.in_current_path(location_to_add):
    print(f"'{location_to_add}' is already in the current PATH.")
else:
    print(f"'{location_to_add}' is NOT in the current PATH.")

print(f"Checking if '{location_to_add}' is in the user's persisted PATH settings...")
if not userpath.in_new_path(location_to_add):
    print(f"Appending '{location_to_add}' to user PATH...")
    userpath.append(location_to_add)
    print(f"'{location_to_add}' has been added to user PATH settings.")
    if userpath.need_shell_restart(location_to_add):
        print("A new shell or system restart might be required for changes to take full effect.")
else:
    print(f"'{location_to_add}' is already configured in the user's PATH settings.")

# To remove a path:
# userpath.remove(location_to_add)

view raw JSON →