views:

91

answers:

7

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

+1  A: 

/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.

troutinator
+1  A: 

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.

jathanism
...for as long as your Python script is running, which is fairly useless.
Wooble
Hah, reading comprehension error on my part. I've updated my answer to reflect the limited scope of usefulness. *shame*
jathanism
+2  A: 

.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.)

ngroot
+2  A: 

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

+1  A: 

~/.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.

unutbu
+1  A: 

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?

Roman Cheplyaka
Well thats what the application is doing, managing "custom" scripts ... When installing this application the user already expects that.
Andrei Ciobanu
+1  A: 

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)
Arrieta