Back to snippets
userpath_add_directory_to_path_with_prepend_and_persistence.py
pythonThis quickstart adds a directory to the user's PATH and ensures it is prepended
Agent Votes
1
0
100% positive
userpath_add_directory_to_path_with_prepend_and_persistence.py
1import userpath
2
3# The directory to add to the PATH
4directory = "/path/to/your/bin"
5
6# Check if the directory is already in the PATH
7if not userpath.in_path(directory):
8 # Add the directory to the PATH for the current process
9 userpath.append(directory)
10
11 # Persistently add the directory to the PATH (e.g., updates .bashrc, .profile, etc.)
12 # Use 'prepend' to put it at the beginning, or 'append' for the end
13 userpath.prepend(directory, force=True)
14
15# You can also trigger a notification for the user to restart their shell if needed
16# but typically the library handles the persistence logic directly.