1
import openpyxl as pyxl
from functions import myFunction

wb = pyxl.load_workbook("data.xlsx")
myDF = pd.DataFrame(columns=cols, index=rows)

The code below works, when I pass in the workbook and dataframe

myFunction(wb, myDF)

but if it doesn't work if I declare global variables within the import function, getting an error of 'NameError: name 'wb' is not defined.' so I don't believe it's recognising the global variable from the main script. Any ideas of what I'm doing wrong?

#from the imported code
myFunction():
  global wb
  global myDF
  ws = wb['Sheet1']

#run from the main script
myFunction()
Josh
  • 157
  • 2
  • 15
  • Not sure if I get what you're saying: So, the variables `wb` and `myDF` are in the same file as `myFunction`? If not, did you import them into the same file? – Patrick Klein Apr 07 '22 at 15:54
  • 3
    `wb` and `myDF` come from the global scope where `myFunction` is *defined*, not the one where it is *called*. "Global" is a bit of a misnomer, in that every module has its own global scope; the only program-wide scope in Python is the built-in scope (which is effectively a read-only scope; you cannot add names to it). – chepner Apr 07 '22 at 15:57
  • Because global means module global. When you do `from module import x` that creates a new variable `x` in the module doing the importing, but global modifications in the imported module affect `module.x`. here's the good news: this is a *good thing* and the approach you were trying to take is an antipattern to begin with – juanpa.arrivillaga Apr 07 '22 at 15:58
  • 1
    Don't use that approach. Don't use `global`. Use parameters and return values like every experienced programmer. – Matthias Apr 07 '22 at 16:01

1 Answers1

1

Globals in Python are global to a module, not across all modules. as explained in this answer Visibility of global variables in imported modules

So you have to put this code inside your functions in order to work:

import openpyxl as pyxl
import pandas as pd

wb = pyxl.load_workbook("data.xlsx")
cols=['A','B'] #for example purposes
rows=range(1,10) #for example purposes
myDF = pd.DataFrame(columns=cols, index=rows)

def myFunction(): 
    global wb
    global myDF
    ws = wb['Sheet1']
    

And in your main module

from functions import myFunction

myFunction()
Pepe N O
  • 1,678
  • 1
  • 7
  • 11