2014-11-25 26 views
-2

嘗試在我的Windows系統中設置客戶端Python腳本,最後我陷入了下面的錯誤,試圖在谷歌和我自己約4小時。但無法找到解決方案。由於我對Python非常陌生,因此無法找到解決方案。Python AttributeError:'模塊'對象沒有屬性'名稱'

請看看下面的代碼和它的錯誤,所以,你可以有解決方案對我來說,

錯誤:

C:\Python26>python C:\xampp\htdocs\cequel-dev\mbtools\main_inject.py 
Traceback (most recent call last): 
    File "C:\xampp\htdocs\cequel-dev\mbtools\main_inject.py", line 12, in <module> 
    import injectdir 
    File "C:\xampp\htdocs\cequel-dev\mbtools\injectdir\__init__.py", line 10, in <module> 
    import action 
    File "C:\xampp\htdocs\cequel-dev\mbtools\injectdir\action\__init__.py", line 1, in <module> 
    from command import list 
    File "C:\xampp\htdocs\cequel-dev\mbtools\injectdir\action\command.py", line 28, in <module> 
    action_list[action.name]=action 
AttributeError: 'module' object has no attribute 'name' 

代碼:(行號:19〜28)

try: 
    action_list={} 
    for file in filenames: 
     if file.endswith('.py') and file != '__init__.py' and file != 'command.py': 
     #Import the file as a module action_imp 
     exec "import {0} as action_imp".format(file[0:-3]) 
     #Get the action object from action_imp. Name is a required method for all actions 
     action=action_imp 
     #Put the file name in file_name 
     action.file_name=file[0:-3] 
     action_list[action.name]=action 
except: 

這看起來像是一個數組屬性錯誤。所以我已經嘗試過,如果條件,但沒有運氣到目前爲止。

所以我堅持最後一行(「action_list [action.name] = action」)。如果您有任何建議或任何快速解決方案來抑制for循環中的錯誤,請讓我知道。

謝謝。

+0

嘗試'hasattr::(模塊對象,名)'

或者,您可以通過更換線28壓制錯誤。 – 2014-11-25 11:44:42

+1

什麼是'action_imp' – 2014-11-25 11:49:08

+0

@VishnuUpadhyay是什麼語法? – 2014-11-25 11:49:29

回答

0

正如exec語句後面的註釋所說,每個動作都必須有一個名稱。

確保filenames__init__.pycommand.py除外)中列出的每個文件都包含變量name

try: 
    action_list[action.name]=action 
except AttributeError: 
    print "Could not register action", action.file_name 
+0

非常感謝我的朋友。錯誤被壓制,現在工作正常。 :) – Raja 2014-11-25 12:18:41

相關問題