0

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)
Zach
  • 3
  • 2
  • 3
    You should `import re` at the beginning of the module, not in `__init__` of the class. – zvone Aug 16 '18 at 18:41
  • I have already tried that. I also put it in the directory's `__init__.py` to be safe. – Zach Aug 16 '18 at 19:01
  • 2
    Don't put it in `__init__.py`. Put it in the file in which you are calling `re.search`. Put it in every file in which you are referencing `re`, otherwise you'll have the error `'re' is not defined`. If you do that, you will not have that error any more. – zvone Aug 16 '18 at 20:29
  • I removed the import call from `__init__.py` and placed it at the top of every module using `re`. I am still getting the same error. Interestingly enough, when I comment out that portion of the code, the error still occurs. I also tried [Michael's solution](https://stackoverflow.com/a/12756257/9743339) and put the import statement into every class method, but nothing changed. – Zach Aug 16 '18 at 23:27
  • Well then show the code which has the import and still has the same error. – zvone Aug 17 '18 at 00:20

0 Answers0