I am trying to split up my module into several modules such that each module only contains one class.
ClassA
is my superclass in file1.py
, ClassB
is located in file2.py
and inherits from ClassA
and ClassC
in file3.py
, and file4.py
contains ClassD
, which inherits from ClassB
, and __main__
.
When I run file4.py
I get the following traceback:
NameError Traceback (most recent call last)
C:\Users\User\Documents\Python\file4.py in <module>()
1016 t0 = time.time()
1017 # Initialize dataframe object to save/export at the end of ClassD.
-> 1018 myData = ClassA(dataframe)
1019
1020 # Initialize ClassC objects.
C:\Users\User\Documents\Python\file1.py in __init__(self, dataframe)
24 self.df.rename(columns = {'%s'%header:'GTIN1'}, inplace = True)
25 if re.search(".MPN", headerID):
---> 26 header = headerID
27 self.df.rename(columns = {'%s'%header:'MPN'}, inplace = True)
28
NameError: global name 're' is not defined
I have tried import re
in both ClassA
's __init__
and in the first line of file1.py
after reading this question. Each file also imports the classes from the other modules in the directory. I have also tried copying re.pyc
to the directory. Is this the solution? I tried adding the current directory to sys.path
in each module, but that did not work either.
Update
Below is the code mentioned in the comments that is still returning the NameError.
import re as re, pandas as pd
from file2 import ClassB as ClassB
from file3 import ClassC as ClassC # ClassC contains static methods that use re
from file4 import ClassD as ClassD
class ClassA(object):
"""Converts dataframe from unicode string objects to Python
string objects, renames column headers.
"""
def __init__(self, dataframe): # dataframe is defined in file4.py in __main__
super(ClassA, self).__init__()
self.df = dataframe.replace({r'[^\x00-\x7F]+':''}, regex=True, inplace=True)
self.df = dataframe.applymap(str)
# I commented out the following and still receive the NameError.
# for headerID in self.df.columns:
# if ('UPC' or 'GTIN1' or 'GTIN' or 'EAN') in headerID:
# header = headerID
# self.df.rename(columns = {'%s'%header:'GTIN1'}, inplace = True)
# if re.search(".MPN", headerID):
# header = headerID
# self.df.rename(columns = {'%s'%header:'MPN'}, inplace = True)