0

I've a seemingly simple problem but can't find/figure out a solution.

I have two files and want to pass a variable from one script to the other, and update a variable in the calling script. I want to do this without modifying file_b:

file_a.py

inputs = {
    "input_1": "value"
}

# Import/run file_b.py

print(outputs)

file_b.py

outputs = inputs.get("input_1")


I've tried this which is very close:

global inputs
inputs = {
    "input_1": "value"
}
global outputs
outputs = []

exec(open("script_b.py").read(), globals())

print(outputs)

The exec method is able to send the variables to file_b but not get the outputs back.

Any help would be appreciated.

Alex
  • 60
  • 8

1 Answers1

0

The example I posted actually does work.

I tried to simplify the args passed to file_b by using this instead which doesn't work:

global inputs
inputs = {
    "input_1": "value"
}
global outputs
outputs = []

exec(open("script_b.py").read(), {"inputs": inputs, "outputs": outputs})

print(outputs)
Alex
  • 60
  • 8
  • Is this an answer to your question? If not, edit your question with this updated information instead. – mercator Jun 12 '20 at 16:02