类启动时导入的模块不能被方法使用

时间:2019-08-09 04:57:01

标签: python pdfminer

我创建了一个类,该类在启动时会导入一些模块,然后在某些类方法中使用这些模块。当我在方法中调用模块时,出现一个NameError,这些模型未定义

class Paper(object):
    '''
    analyse a paper and extract parts of it
    '''
    def __init__(self, paper_path):

        #import needed modules
        needed_modules = ['io', 'pdfminer', 'pickle', 're', 'string', 'os']
        import io, pickle, re, string, os

        from pdfminer.converter import TextConverter as TC
        from pdfminer.pdfinterp import PDFPageInterpreter as PInt
        from pdfminer.pdfinterp import PDFResourceManager as RM
        from pdfminer.pdfpage import PDFPage as Page
        from pdfminer.layout import LAParams as LAP

        self._pPath = paper_path


    def get_text(self):
        res_manager = RM()
        fake_file_handle = io.StringIO()
        codec = 'utf-8'
        laparams = LAP()
        converter = TextConverter(res_manager,
        fake_file_handle,codec=codec, laparams=laparams)
        page_interpreter = PInt(res_manager, converter)

        with open(self._pPath, 'rb') as fh:
            for page in Page.get_pages(fh,
                                          caching=True,
                                          check_extractable=True):
                page_interpreter.process_page(page)

            text = fake_file_handle.getvalue()

        # close open handles
        converter.close()
        fake_file_handle.close()

        if text:
            return text

运行此代码时:

pPath = 'path'

item = Paper(pPath)
print(len(item.get_text()))

我收到以下错误

NameError: name 'RM' is not defined

2 个答案:

答案 0 :(得分:0)

我相信模块是作为init函数中的本地对象创建的。如果将导入语句移到初始化方法的外部,则它应该起作用。只是一直导入到python文件顶部(在任何方法之外)都是很常见的

答案 1 :(得分:0)

正如Harm所说:导入对于您的__init__方法是本地的。您需要将它们向上移动或存储到某个变量中,然后在每种方法中更新为locals()

一个非常难看的骇客:

def __init__(self, paper_path):
    ...
    self.imports = locals()  # possibly only include the imports, not the entire dict

def get_text(self):
    locals().update(self.locals)