0

我在“Sample.py”的python“Sample.py”和“GenericFunctions.py”中编写了两个模块,我正在调用一个函数

存在于“GenericFunctions.py”中,但我无法将该函数调用为“AttributeError:'module'对象没有属性'fn_ElseLog'”

Sample.py 中的代码:

import GenericFunctions

def sampleee():
    g_TCaseID="SD1233"
    g_TCDescription="Login"
    g_TestData="hi"
    g_Result="Fail"

    g_Remarks="testing"
    g_TargetEnvironment="S1"
    g_TargetSystem="Legacy"
    g_TargetRegion="EU"
    x = GenericFunctions.fn_ElseLog(g_TCaseID, g_TCDescription, g_TestData, g_Result, g_Remarks)


sampleee()

GenericFunctions.py 中的代码:

def fn_ElseLog(g_TCaseID, g_TCDescription, g_TestData, g_Result, g_Remarks):

    print "entered in ElseLog Function"
    Output= fn_Output(g_TCaseID, g_TCDescription, g_TestData, g_Result , g_Remarks)
    print ("Testcase"+"'"+g_TCDescription+"'"+"execution completed"+"'"+g_TargetEnvironment+"-"+g_TargetRegion)

def fn_Output(p_TCaseID, p_TCDescription, p_TestData, p_Result , p_Remarks):
    OutputSheet=""
    OutputSheet="\Test"+"_"+g_TargetEnvironment+"_"+g_TargetSystem+"_"+g_TargetRegion+".xlsx" 
    OutputPath=r"C:\Users\u304080\Desktop\PlayAround\Shakedowns\OutputResultFiles"

    #objExcel1 = win32.gencache.EnsureDispatch('Excel.Application')
    Outputfile=os.path.exists(OutputPath+OutputSheet)

    if Outputfile==True :
       print('Output file is present')
    else:
       print('Output file is not present')
       return

    objExceloutput = win32.gencache.EnsureDispatch('Excel.Application')
     #excel.DisplayAlerts = False
    objoutputworkbook = objExceloutput.Workbooks.Open(OutputPath+OutputSheet)
    objSheetOutput = objoutputworkbook.Sheets(1)
    OutputRowCount =objSheetOutput.UsedRange.Rows.Count
    print "OutputRowcount" , OutputRowCount
    objSheetOutput.Cells(OutputRowCount+1,1).Value=p_TCaseID
    objSheetOutput.Cells(OutputRowCount+1,2).Value=p_TCDescription
    objSheetOutput.Cells(OutputRowCount+1,3).Value=p_TestData
    objSheetOutput.Cells(OutputRowCount+1,4).Value=p_Result
    objSheetOutput.Cells(OutputRowCount+1,4).Font.Bold = True

    if p_Result=="Pass":
       objSheetOutput.Cells(OutputRowCount+1,1).Font.ColorIndex = 10 
    else:
       objSheetOutput.Cells(OutputRowCount+1,1).Font.ColorIndex = 3

    objoutputworkbook.SaveAs(OutputPath)
    objSheetOutput=None
    objoutputworkbook=None
    objExceloutput.Quit()
    objExceloutput=None   

你们能告诉我一个解决方案吗?

4

2 回答 2

1

存在于“GenericFunctions.py”中,但我无法将该函数调用为“AttributeError:'module'对象没有属性'fn_ElseLog'”

如前所述,您的代码看起来是正确的。

检查以确保两个文件位于同一目录中,然后重新启动 python 或执行reload(GenericFunctions)以确保sys.modules缓存中没有过期版本。

于 2013-11-04T08:36:28.207 回答
1

尝试:

  1. 删除pyc目录中的所有文件。(以确保它不在缓存中)
  2. print(dir(GenericFunctions))print(GenericFunctions.__file__)在 中Sample.py,查看您是否正在导入您认为正在导入的文件。
  3. GenericFunctions看看你的PYTHONPATH(echo $PYTHONPATH) 上是否有其他文件被调用。
于 2013-11-04T08:36:35.863 回答