-1

I have two .py files, one is Setting.py and one is main.py.

In the Setting.py file I have the following code:

a= 2
def foo(a): 
  return a+2

In the main file I have this:

import Setting

if __name__ == '__main__':

    X = foo(a)

So, when I run the main file, I get the error that a is not defined. So, I dont want again to define a in the main file. Could you please help me with this? this is a minimal code of my main code. I have multiple .py files and parameters. Thanks

Sadcow
  • 680
  • 5
  • 13
  • 1
    `from Setting import a`. Even then, it's hit and hope with your minimal example – roganjosh Mar 30 '23 at 18:38
  • 2
    You can either say `X = Setting.foo(Setting.a)`, or you can use `from Setting import foo, a`. – Tim Roberts Mar 30 '23 at 18:38
  • You could define `foo` as `def foo(a=a):` and then call it with `X = foo()`. That would still give you the option of redefining `a` by passing a different argument in. – Axe319 Mar 30 '23 at 18:40
  • Actually, _both_ are imported from `Setting`. This is a design flaw (you know that `main` doesn't mean anything in Python in terms of user-defined module/function names?). `if __name__ == '__main__'` does not relate to the filenames you've given or the function names – roganjosh Mar 30 '23 at 18:40

1 Answers1

1

You could try either from Setting import * or from Setting import foo,a.

River
  • 47
  • 7