I have the below code which is not returning the dataframe created in the main module. Can someone help what could be the issue. If not in main() the function works fine. I am unsure on what is being missed here. I am testing this code in Jupyter notebook and trying to call exceldf in a new cell
import package as pkg
def main():
list1, list2= pkg.somefunction()
exceldf = pkg.functiontoloadexcelfiles.function(parameter1, parameter2)
I am expecting to see the data in exceldf however I get the error "name 'exceldf' is not defined".
Edit :
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_26012/684895253.py in <module>
----> 1 exceldf
NameError: name 'exceldf' is not defined
The above code runs fine when not wrapped in main function.
list1,list2= pkg.somefunction()
exceldf = pkg.functiontoloadexcelfiles.function(parameter1,parameter2)
exceldf.head()
The below fixed the issue
import package as pkg
def main():
global exceldf
list1, list2= pkg.somefunction()
exceldf = pkg.functiontoloadexcelfiles.function(parameter1, parameter2)
>Solution :
exceldf is only locally declared within main() so you must either declare exceldf as a global variable or return exceldf from the main() function.
import package as pkg
def main():
list1, list2= pkg.somefunction()
exceldf = pkg.functiontoloadexcelfiles.function(parameter1, parameter2)
return exceldf
exceldf = main()
exceldf.head()