Andrei Ciobanu
Andrei Ciobanu

Reputation: 12858

How to update $PATH

I am writing a python/pygtk application that is adding some custom scripts (bash) in a certain folder in $HOME (eg. ~/.custom_scripts).

I want to make that folder available in $PATH. So every time the python app is adding the script, that script could be instantly available when the user is opening a terminal (eg. gnome-terminal).

Where do you suggest to "inject" that $PATH dependecy ? .bashrc, /etc/profile.d, etc. ? What advantages / disadvantages I might encounter ?

For example if i add a script to export the new path in /etc/profile.d, the path is not being updated until I re-login.

Thanks

Upvotes: 3

Views: 4027

Answers (7)

Escualo
Escualo

Reputation: 42182

Why don't you establish the appropriate PATH upon the first call to your module (i.e. in your module's __init__.py):

# this is your module's __init__.py
import sys
eggs = ['/path/to/egg/1.egg', '/path/to/egg/2.egg']
for egg in eggs:
    sys.path.append(egg)

Upvotes: -1

jathanism
jathanism

Reputation: 33724

Edit: I misread the original question, so this snippet is only useful for modifying PATH, but not for persisting it...

This can all be done using the os module:

import os
USER_HOME = os.path.expanduser('~')
os.environ['PATH'] += ":" + os.path.join(USER_HOME, '.custom_scripts')

This appends :~/.custom_scripts to the end of the $PATH, since PATH must always be colon-delimited.

Upvotes: 1

Roman Cheplyaka
Roman Cheplyaka

Reputation: 38768

You shouldn't. It's the user choice whether he wants that in the PATH, in what cases and how to achieve that. What you can do is inform the user about the directory where your scripts reside and suggest putting it to the PATH.

Or maybe you're asking from the user's perspective?

Upvotes: 1

unutbu
unutbu

Reputation: 880887

~/.bashrc is read every time gnome-terminal is opened, (assuming the user has SHELL set to /bin/bash).

Be sure to check os.environ['PATH'] to see if the directory has already been added, so that the script doesn't add it more than once.

Upvotes: 1

user268396
user268396

Reputation: 12006

For scripts that go in the $HOME directory you'd typically use $HOME/bin folder instead which is (usually) on the path.

Upvotes: 2

ngroot
ngroot

Reputation: 1176

.profile would be a reasonable place if it's a per-user install; /etc/profile.d for system-wide installs. (You'll need root to do that, of course.)

Your installer won't be able to change the path of the current shell (unless it's being run via source, which would be...odd.)

Upvotes: 2

troutinator
troutinator

Reputation: 1198

/etc/profile.d would add it to every user's path

~/.bashrc would just be your own

you can always do "$ source ~/.bashrc" to re-read the config files.

Upvotes: 1

Related Questions