title: Error despite Global keyword being used to access variable inside function
body: <p>I have created few global variables as shown in my code below. However when I try to use them inside the individual functions, I still get the same error.</p>
  
  <p>Please find my code below</p>
  
  <pre><code>def create_df():
    global sheet_name, sheet_df 
    for s in sheets:
       sheet_name = s
       sheet_df = pd.read_excel(xls, sheet_name=s)
       sheet_df = sheet_df[sheet_df.columns.difference(sheet_df.filter(like='Derived').columns,sort=False)]
       print("Sheet " + str(s) + " is created as a dataframe successfully")
       transform_stage_1_df()
  
  def transform_stage_1_df():
      global sheet_df 
      sheet_df = pd.melt(sheet_df, id_vars='subject_ID', var_name='headers', value_name='dates')
      sheet_df['header_extracted'] = [x.split("Date")[0] for x in sheet_df['headers']]
      sheet_df['day'] = sheet_df['header_extracted'].str.extract('(\d+)', expand=True).astype(int)
      sheet_df = sheet_df[sheet_df.groupby(['subject_ID','header_extracted'])['dates'].transform('count').ne(0)].copy()
      sheet_df = sheet_df.sort_values(by=['subject_ID','day'])
      sheet_df.drop(['header_extracted', 'day'], axis=1, inplace=True)
  print("Stage 1 transformation is complete")
  
  
  if __name__ == '__main__':
      print("Execution Started")
      print("File read successfully")
      sheets = xls.sheet_names
      sheet_name = sheet_df = Non_Cholesterol = None
      dataFramesDict = dict()
      create_df()
      add_units()
      Non_Cholesterol.to_csv('Output.csv')
  </code></pre>
  
  <p>Based on SO posts, I have already updated the global keyword but still I get the <code>UnboundLocalError: local variable 'sheet_df' referenced before assignment</code></p>
  
  <pre><code>UnboundLocalError                         Traceback (most recent call last)
   &lt;ipython-input-210-dc2f8412235d&gt; in &lt;module&gt;
        7     sheet_df = None
         8     dataFramesDict = dict()
    ----&gt; 9     create_df()
   10     Non_Cholesterol.to_csv('C:\\Users' + 
   str('Non_cholesterol.csv'),index=None)
   11     print("Export successful")
  
   &lt;ipython-input-205-c93604f0da4f&gt; in create_df()
    5         sheet_df =sheet_df[sheet_df.columns.difference(sheet_df.filter(like='Derived').columns,sort=False)]
    6         print("Sheet " + str(s) + " is created as a dataframe 
    successfully")
       ----&gt; 7         transform_stage_1_df()
  
   &lt;ipython-input-206-b59c70018a9b&gt; in transform_stage_1_df()
    1 def transform_stage_1_df():
    ----&gt; 2     sheet_df = pd.melt(sheet_df, id_vars='subject_ID', 
   var_name='headers', value_name='dates')
    3     sheet_df['header_extracted'] = [x.split("Date")[0] for x in 
   sheet_df['headers']]
    4     sheet_df['day'] = sheet_df['header_extracted'].str.extract('(\d+)', 
   expand=True).astype(int)
    5     sheet_df = 
   sheet_df[sheet_df.groupby(['subject_ID','header_extracted']) 
   ['dates'].transform('count').ne(0)].copy()
  
    UnboundLocalError: local variable 'sheet_df' referenced before assignment
  </code></pre>