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)
CodePudding user response:
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()
CodePudding user response:
check your code a little more for instances of where exceldf is used, it seems like there is a use of it that is outside the scope of the main function where the value is allocated, it could just be a whitespace issue. do you call exceldf.head()
after your main function like this?
def main():
list1, list2= pkg.somefunction()
exceldf = pkg.functiontoloadexcelfiles.function(parameter1, parameter2)
exceldf.head()
if so, the main function was never called and it has no return statement so the interpreter doesn't know about the exceldf
name yet but you are trying to call it, maybe try something like this:
def create_excel_df():
list1, list2= pkg.somefunction()
exceldf = pkg.functiontoloadexcelfiles.function(parameter1, parameter2)
return exceldf
exceldf = create_excel_df()
exceldf.head()
i changed the name of your main
function to something more descriptive because main
would only really ever be used to run a main loop in a program while you're using a jupyter notebook more like a data pipeline intented for one of runs instead of continuous execution