If you perform the assignment of a variable inside a function, the global variable will be ignored and will not be available when the function is executed, in the sample with mathlib you do not redefine the name math, so it works. Snipped below will give you the same error with math lib:
import math
def my_function():
  print(math.pi)
  math = 1
my_function()
global , - , .
import math
def my_function():
  global math
  print(math.pi)
  math = 1
print(math) 
my_function() 
print(math)