I currently have a python file with a bunch of global variables with values. I want to change these values permanently from a separate python script. I've tried setattr and such but it doesnt seem to work. Is there a way to do this?
-
Do you mean that you want to actually modify the one script from a different one? – alternative Aug 23 '10 at 20:50
-
I wonder if the standard library has the capability to parse a Python source file in memory, change it, and then write that the a file? – Humphrey Bogart Aug 23 '10 at 20:51
-
Your other question is off-topic. Please **do not mention it here**. Please delete the reference to an "other question". Please focus. – S.Lott Aug 23 '10 at 20:52
-
http://stackoverflow.com/questions/768634/python-parse-a-py-file-read-the-ast-modify-it-then-write-back-the-modified – Daniel Kluev Aug 23 '10 at 21:11
2 Answers
The short answer is: don't. It won't be worth the trouble.
It sounds like you are trying to create a configuration file and then have your application update it. You should try using ConfigParser, a built-in module that can read and write configuration files for you with limited hassle: http://docs.python.org/library/configparser.html

- 49,756
- 17
- 74
- 82
-
Well this is precisely what I am trying to do. I guess if there is no other way, configparser it is... – lango Aug 23 '10 at 20:58
-
1@Iango, there is another way, but it's going to be more trouble than it's worth. – carl Aug 23 '10 at 21:02
currently have a python file with a bunch of global variables with values
Let's pretend it looks like this. globals.py
this = 1
that = 2
And there's nothing else in this file. Nothing.
Let's further pretend that this file is used as follows.
from globals import *
Let's further pretend that we have some simulation which needs to "update" globals.py
import os
os.rename( "globals.py", "globals.bak" )
with open( "globals.py", "w" ) as target:
for variable in ('some', 'list', 'of', 'sensible', 'globals'):
target.write( "{0!s} = {1!r}".format( variable, globals()[variable] )
Basically, you recreate Python code from your global dictionary.
This is a dreadful solution. Please don't actually use it.

- 384,516
- 81
- 508
- 779
-
I created the relevant python files with your above code to test it and I get this error: python test.py Traceback (most recent call last): File "test.py", line 7, in
for variable in globals: TypeError: 'builtin_function_or_method' object is not iterable Am I doing something wrong? – lango Aug 23 '10 at 21:17 -
1Yes. You are doing something wrong. You are typing without thinking. Sometimes posted code has errors. You need to think for just a second before typing to see if you understand the code. – S.Lott Aug 23 '10 at 21:18
-
1Maybe I just don't understand python well enough since I am relatively new to it? I typed your code out, was thinking through it, caught the missing parens at the end. But I thought globals (before your edit) referred to the file or maybe some built-in python variable. Anyways, thank you for your solution. I was trying out both yours and @carl's. – lango Aug 23 '10 at 21:38