2

I have two Python files, global.py and test_train.py, both sitting in the same folder. I am trying to allow test_train.py to access everything (functions, variables, etc. in global.py). I cannot get it to work. In the same folder is init.py. I have tried import global and have also referenced Source a Python's file from another Python file, but nothing seems to work. Suggestions?

2 Answers 2

1

You should change your file (global.py) name, when you import global.py, the module name global will conflict with python keyword global

import global  # global is keyword in python, you should avoid using this name
               # so it will occur error
import global_test  # this line is OK, if your .py name is global_test
Sign up to request clarification or add additional context in comments.

Comments

1

Typically import global should work. However, if it doesn't work, you should do a check to see if the import is happening successfully.
A print statement at the end of the global.py script should suffice to tell you so.

Otherwise, if the import is working, then you should try changing your code by encapsulating the entire contents of the global.py in a class, and then creating an object of that class in your test_train.py script to call its functions and attributes.

You can also make use of getattr function to call directly from global.py

Comments

Your Answer

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