2016-11-07 59 views
-1

我有一個名爲customization.pyPython的 - 不能導入名稱「字符」

import sys 
from easyread import * 
import main 
class character(): 

    def __init__(self, role): 
    self.role = role 


    def setRole(self): 
    print """ 
    What role do you choose? 
    \033[31m Warrior 
    \033[35m Mage 
    \033[30m Rougue 
    \033[33m Healer 
    \033[0m """ 
    charRole = raw_input() 

    if charRole == "Warrior": 
     self.role = "Warrior" 
    elif charRole == "Mage": 
     self.role = "Mage" 
    elif charRole == "Rouge": 
     self.role = "Rouge" 
    elif charRole == "Healer": 
     self.role = "Healer" 
    else: 
     sys.exit() 

    wait(0.5) 

    print "You chose %s" % self.role 

我試圖使用類名爲main.py

import sys 
from easyread import * 
from customization import character 

char = customization.character(role = "") 
不同的文件的文件這個Python代碼

但試圖運行時出現此錯誤。

回溯(最近通話最後一個):
文件 「main.py」,3號線,在

from customization import char                               File "/home/cabox/workspace/customization.py", line 3, in <module>  

import main                                    File "/home/cabox/workspace/main.py", line 3, in <module>    

from customization import char                               ImportError: cannot import name char 
+2

您有一個循環導入。 'main'導入'定製',反之亦然。在python中查找如何解決循環導入,有多種方法。 –

+0

您應該從'main'模塊中導入'char',而不是從'customization'模塊導入。 –

回答

2

你可以嘗試刪除循環依賴。 In customization.py you imports main,and in main.py,you import customization。從customization.py刪除import main

此外,您從customization導入character,所以要使用它,您只需要character(role = "")