2

I have not been able to find a reason why the matter does not work for me, so I would like to ask a question here.

I have 2 files:

file2.py:

def test():
    global justTry
    justTry = "hello"

and main.py:

from file2 import *

def main():
    print(justTry)

if __name__ == '__main__':
    test()
    main()

And I am getting the error: NameError: name 'justTry' is not defined. Why can't I use the justTry variable, which I declared as a global variable in the step before the listing?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
xHybrid
  • 23
  • 1
  • 4
  • Is the `test()` function called, which initialises the global? Or, is the global initialised outside the function? (And can I interject that global *variables* are categorically a "bad idea", and suggest a more robust approach might be available.) – S3DEV Jun 27 '21 at 14:56
  • @pinkfloydx33 - I tried that. I assigned "global justTry" in the main () function, but still the same error. – xHybrid Jun 27 '21 at 14:58
  • @S3DEV I understand it's not the best idea, but I'd like to know why it is not working. The test() function is called 100%, I tried the statement directly from the function and everything works properly. – xHybrid Jun 27 '21 at 15:01
  • This is *probably* because the variable does not yet exist when the `import *` is executed, but I'll leave it to someone else to write an actual answer and explain the semantics in detail... – Ture Pålsson Jun 27 '21 at 15:29
  • The `global` keyword defines a variable as global ***in the context of the module it is in***, not between modules. Also, from the official Python FAQ: [How do I share global variables across modules?](https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules) – Tomerikoo Jun 27 '21 at 16:31
  • `global` also does not *define* a variable; it just indicates what scope an assignment to a name will use *if* you assign to it. Also, don't use `from .... import *`. – chepner Jun 27 '21 at 16:34
  • In `main`, `from file2 import *` adds the module-level name bindings in `file2` to the namespace of `main`. But at that point `file2.justTry` does not yet exist, so `import *` does not include it. Then your code calls `test()` which creates a name binding at module level in`file2`. But at that point, it is too late for that binding to have an effect on the namespace of `main` because the `import *` has already happened. Python `import`s are executable statements, not declarations, so the order of execution matters. This is why doing `from ... import *` just to save keystrokes is a bad idea. – BoarGules Jun 27 '21 at 16:37

1 Answers1

3

when "*importing" something, it executes the code, and copy's the globals in to your globals. but if globals get alterd later, it won't re-copy the globals. the solution is, to re-import the file after test is called

file2.py:

def test():
    global justTry
    justTry = "hello"

main.py:

from file2 import *

def main():
    print(justTry)

if __name__ == '__main__':
    test()
    from file2 import *
    main()
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
jp_
  • 83
  • 9