0

I would like to run a file1, from another file2, while changing one of its variables. Something like this:

file1.py

a = 1
b = 1
c = 1
# Then:
# Countless functions depending on a, b, c, etc., depending on each other in complicated ways.
# Results from these functions are plotted and exported

file2.py

# Pseudo-code:
import file1 with (a = 0)  # runs file1, exporting all results, as if file1 had a = 0.

What is the simplest way to do this, considering I have too many variables, complicated functions and dependencies in file1.py?

2
  • 1
    Can you change file1.py to read a configuration or at least get its variables from a common module that you can import first? Changing file1.a after import can only effect uses of a after that point. file1.py could use some refactoring. Commented Mar 17, 2020 at 17:14
  • Yes I can. I also believe this is the best option, but not so pythonic. Commented Mar 17, 2020 at 17:23

2 Answers 2

2

If you must change a global variable from another module, you can do so by writing

import file1
file1.a = 0

Note that the import statement will result in the execution of file1 in its entirety, and so any module level usage of a by file1 or its dependencies will use the original value (in this case, 1).

If you need to have control over when the contents of file1 are executed, you should move its contents to a function or class. It seems quite likely that the variable a in your example would be better expressed as a parameter to a function that file1 defines.

Sign up to request clarification or add additional context in comments.

7 Comments

I upvoted, but want to highlight that any use of a at file1.py s module level (e.g, foo = [None] * a) or by any other module that imported file1 before you did, will cause this solution to fail.
As soon as I do "import file1", file1 is run with a = 1, plotting and exporting all results with a = 1, instead of with a = 0.
@BernardoCosta Yes, that is the expected behavior. file1 will be run in its entirety upon being imported. If you need to interactively control its state, you'll need to put all of your code in a function/class.
This is what I want to avoid, because I have 2000 lines of code with dozens of variables and functions. I am considering to put variable "a" in a external text file. Then accessing it from file1. Then, from file2, make this external a=0, and import file1.
@BernardoCosta It sounds like you have a lot of refactoring to do then. As a quick fix, you may be able to wrap the entire script in a main function, which either accepts a as a parameter or continues to use it as a global.
|
0

Wrap your variables as functions, and pass in the value as a parameter.

def get_a(val=1):
    return val

then in file2

import file1
file1.get_a(0)

1 Comment

This doesn't help with other users of file.a. I think this is just a complicated way for file2 to do a = 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.