I have several globals variables in the begining of the Python script and several Classes which use these variables in some methods. I want to keep classes in separate files and load them in main script, but this doesn't work as I expect (and as it works in PHP).
Example of problem:
main.py (the one I run for execution)
global_var = 'my global var value'
from myimport import *
print_global()
Output:
I am loaded!
Traceback (most recent call last):
File "import.py", line 3, in <module>
print_global()
File "myimport.py", line 3, in print_global
print(global_var)
NameError: global name 'global_var' is not defined
myimport.py
print('I am loaded!')
def print_global():
print(global_var)
If I write instruction to import all code (*) into the current namespace, why does function starts in separate module namespace, where global_var is underfined, but not in the name space where it is imported to?
What is the nice Pythonic way to do code separation as I described?