Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python main function not returning the dataframe

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---------------------------------------------------------------------------
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()
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading