Updating the variable values in a python script from other python script

what is the best way to update the value of certain variables in a python script from another python script? the python script that we are trying to update is used by a lot of other scripts from other people so import my not work in this case is what I am thinking?
Any suggestions?

Do you mean over the network? On different computers? Or the same
program but with files provided by different users?
Is this a class based program?

What do you mean by “update the value of certain variables”?

Do you mean that you’re editing the hardcoded values in the script and then running it, or do you want to update their values while it’s running?

Not over the network but python files in different git repos
For example: I have a python script lets say a.py:
`import os
var1 = “a”
var2 = “b”
’
a.py is used by multiple other scripts to do multiple other functions. Now, I have another script lets say b.py that should read the values of var1 and var2 and change the values of these variables in such a way that var1 in a.py now has the value “changeda” and var now has the value “changedb”.
After this change a.py now looks like the following:

import os
var1 = "changeda"
var2 = "changedb"

Now this is an XY problem if I ever saw one.

What are you actually trying to do? Whatever it is, programmatically editing Python scripts is not the right solution.

1 Like

Hi,

if you have files a.py and b.py, then you can change the values of file a via
the method shown below. This is assuming that both files are in the same directory.
I don’t know how to change values that are in different git hubs, however.

'''  a.py    '''

var1 = 'a'
var2 = 'b'

def change_var1(newVar1):
    global var1
    var1 = newVar1

def change_var2(newVar2):
    global var2
    var2 = newVar2   

- * - *- *- *- * - *- *- *- *- *- 

'''  b.py    '''

import a

# Values before change
print('The value of var1 before change is: ', a.var1)
print('The value of var2 before change is: ', a.var2)
    
a.change_var1('changeda')
a.change_var2('changedb')

# Values after change
print('\nThe value of var1 after change is: ', a.var1)
print('The value of var2 after change is: ', a.var2)

See if that helps you.

This sounds like you want to modify the script itself. We almost never
do this.

What’s your larger objective?

The usual approach, if I understand your intent, is to have a
configuration file or environment variable of some kind. So maybe you’ve
got a.py like this:

 var1 = "a"

as a default. You might make it honour an environment variable:

 var1 = os.get("APY_VAR1", "a")

so that if the environment variable $APY_VAR1 were set to something,
that would be used instead of the default "a".

Or you might have a configuation file, for example an “ini” file:

 [apy]
 var1 = something

You could use the configparser module to parse such a file:

Maybe this isn’t what you’re after, but if you’'re after tunable
configuration stuff, the bare "a" values in your code are intended to
provide value sfor when there’s no configuration at all, and you put
stuff into the code to let a configuration file override those defaults.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Thank you everyone, the recommendations were really helpful. Another question in follow up to the recommendations I tried:
lets say I define two global variables var1 and var2 in a.py and some function in a.py changes the value of var1 and var2 at runtime. now I want to get the updated values of var1 and var2 in b.py. How can I make sure when b.y imports those variable values, those values are the updated values.

If you’re storing the values in a config file, a.py would read that file when it starts in order to get the initial values, and then, when it’s about to quit, it would write the final values back to the file.

b.py can then read the file when it starts in order to get the values.

Proposition:

  1. Read values var1 and var2 in a.py prior to calling function modifying the two values.
    The called function can potentially return the before and after values in a tuple so that
    the before and after values may be compared.
  2. Call function to modify var1 and var2 in a.py
  3. Read values from module b.py
  4. Code in module b.py can perform comparison with if conditional statements.

For example:

   if (var1_pre, var2_pre) == (var1_post, var2_post):

Try your code in that order.