[Tutor] Set LD_LIBRARY_PATH and equivalents platform-independently

Alan Gauld alan.gauld at btinternet.com
Thu Jan 17 19:10:29 CET 2013


On 17/01/13 12:14, Albert-Jan Roskam wrote:
>
> Thanks for your replies. os.putenv() may be easier than os.environ because,
 > hopefully, it takes care of the OS-specific separators of the values
 > (";" for Windows, ":" for Linux, others I don't know

I wouldn't count on it. Support for changing environment variables on 
the fly is iffy in most languages and is not something I'd ever do lightly.

The environment is something that usually should be set up when the user 
logs in to reflect their preferences, its not really polite of a program 
to try and alter the users environment...

> Then again, this sentence from the Python page is a little worrying:
 > "*If* the platform supports the putenv() function, ...".

Not all OS allow changes to the environment variables. Others only allow 
the value to be a fixed size allocated at startup and if you write more 
bytes than are already assigned you will overwrite the next value! The 
OS assumes that this stuff is set up once and not changed.

putenv() should always be considered a platform specific bit of code.


> As an alternative, I used os.chdir to tell the OS where to start
 > looking for libraries, but somebody on this list (I forgot who)
 > warned me that this could have nasty side effects.

Again the user may expect to be in a certain directory so as a minimum 
you need to change back to where they were when you exit.

The normal way to deal with local file locations is to have a resource 
file that's read when the program starts up - like .vimrc or .emacs
Then if the file doesn't exist you either create it at install time or 
use default values and allow the user to override them.

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list