Beginner here. I'm trying to learn about global/local variables, circular imports, and how to avoid them. I have three files:
#globaltest import pandas as pd x = 0 dic = {'1995': [3,5,2,1,2,7], '1996': [6,3,1,1,1,8], } df = pd.DataFrame(dic, columns = ['1995','1996'], index=['AT1','AT2','AT3','FR1','FR5','FR7']) df.index.name='Geo' Second file is:
#prueba from globaltest import x,df def abcde(): #from globaltest import x,df while x<5: print("Not yet") x=x+1 print("Now ",df) And the third file is:
#main from prueba import abcde ab=int(input("Option: ")) if ab==1: abcde() The code works well if in the second file, "prueba", I import the variables from "globaltest" inside of the function abcde(). If I import the variables at the top, however, it doesn't work (UnboundLocalError: local variable 'x' referenced before assignment). I've read that you should not import anything inside functions, so I would like to know what would be the solution to avoid doing it in this case. Thanks in advance!