views:

787

answers:

9

From what I've read, any changes to the environment variables in a Python instance are only available within that instance, and disappear once the instance is closed. Is there any way to make them stick by committing them to the system?

The reason I need to do this is because at the studio where I work, tools like Maya rely heavily on environment variables to configure paths across multiple platforms.

My test code is

import os
os.environ['FAKE'] = 'C:\\'

Opening another instance of Python and requesting os.environ['FAKE'] yields a KeyError.

NOTE: Portability will be an issue, but the small API I'm writing will be able to check OS version and trigger different commands if necessary.

That said, I've gone the route of using the Windows registry technique and will simply write alternative methods that will call shell scripts on other platforms as they become requirements.

+2  A: 

According to this discussion, you cannot do it. What are you trying to accomplish?

Brian
I'm literally trying to make an environment switcher that adjusts environment variables across several platforms.
Soviut
@Soviut, Why are you trying to change environment variables?
Zoredache
Its so we can switch between different projects that our 3D applications use. They rely heavily on environment variables.
Soviut
Please update your question with these additional facts.
S.Lott
+2  A: 

You are forking a new process and cannot change the environment of the parent process as you cannot do if you start a new shell process from the shell

leancz
+3  A: 

I don't believe you can do this; there are two work-arounds I can think of.

  1. The os.putenv function sets the environment for processes you start with, i.e. os.system, popen, etc. Depending on what you're trying to do, perhaps you could have one master Python instance that sets the variable, and then spawns new instances.

  2. You could run a shell script or batch file to set it for you, but that becomes much less portable. See this article:

http://code.activestate.com/recipes/159462/

DNS
+4  A: 

Under Windows it's possible for you to make changes to environment variables persistent via the registry with this recipe, though it seems like overkill.

To echo Brian's question, what are you trying to accomplish? There is probably an easier way.

Jeff Bauer
+3  A: 

make them stick by committing them to the system?

I think you are a bit confused here. There is no 'system' environment. Each process has their own environment as part its memory. A process can only change its own environment. A process can set the initial environment for processes it creates.

If you really do think you need to set environment variables for the system you will need to look at changing them in the location they get initially loaded from like the registry on windows or your shell configuration file on Linux.

Zoredache
By 'system' environment variables, Soviut is referring to variables that are established system-wide, e.g. often /etc/profile in Unix or as a System (vs. User) environment variable in Windows. I think this is clear from his context.
Jeff Bauer
That's what I mean.
Soviut
+1  A: 

You might want to try Python Win32 Extensions,developed by Mark Hammond, which is included in the activestate installer (or can be installed separately). You can learn how to perform many Windows related tasks in Hammond's and Robinson's book.

Using Pywin32 to access windows COM objects, a python program can use the Environment Property (a collection of environment variables) of the WScript.Shell object.

gimel
+1  A: 

Think about it this way.

You're not setting shell environment variables.

You're spawning a subshell with some given environment variable settings; this subshell runs your application with the modified environment.

S.Lott
A: 

From within Python? No, it can't be done!

If you are not bound to Python, you should consider using shell scripts (sh, bash, etc). The "source" command allows you to run a script that modifies the environment and will "stick" like you want to the shell you "sourced" the script in. What's going on here is that the shell executes the script directly rather creating a sub-process to execute the script.

This will be quite portable - you can use cygwin on windows to do this.

Daniel Paull
The idea was to keep things as contained as possible within Python, not install a huge library like cygwin just to get a few bash commands out of it.
Soviut
A: 

Look here.