Reputation: 83
When I define and call a function within a script it works great. When I import it from an adjacent file I get an error: "UnboundLocalError: local variable 'data' referenced before assignment"
I have tried including the "return(json_normalize(data))" in the IF statement but it didn't return anything.
The function being imported.
def get_query(query_num):
if __name__ == '__main__':
params = {'p_param': query_num}
query_id = query_num
data = get_fresh_query_result('https://redash.domain.io', query_id, api_key, params)
return(json_normalize(data))
get_fresh_query_result is working fine when imported.
The expected result is a Pandas df. but I am getting an error instead!
Upvotes: 0
Views: 62
Reputation: 421
The error occurs because of the if statement checking whether the current module is __main__
. This is only the case if the function is in the main script. Remove the if
statement, and the module can be imported from other places.
Upvotes: 3