2013-07-08 241 views
6

我想在Python中使用turtle模塊。但是,當我做進口龜模塊,我以下錯誤:無法在Python 2.x和Python 3.x中導入turtle模塊

$ python 
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import turtle 
Traceback (most recent call last):                  
    File "<stdin>", line 1, in <module>                   
    File "turtle.py", line 3, in <module>                     
    myTurtle = turtle.Turtle()                         
AttributeError: 'module' object has no attribute 'Turtle' 

和Python 3.x都有:

$ python3 
Python 3.2.3 (default, Sep 30 2012, 16:41:36) 
[GCC 4.7.2] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import turtle 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "turtle.py", line 3, in <module> 
    myTurtle = turtle.Turtle() 
AttributeError: 'module' object has no attribute 'Turtle' 

下我的Kubuntu的Linux 12.10工作。我和Tkinter gui一起玩過。沒有問題。烏龜模塊會發生什麼?

回答

20

您已經調用了腳本turtle.py,該腳本將標準庫中的turtle模塊遮蔽起來。重命名它。

+0

哦......我犯了一個愚蠢的錯誤。非常感謝你。 –

+0

你能解釋一下嗎? – Stephan

+2

當前目錄中有文件turtle.py。我嘗試導入烏龜模塊。和python解釋器搜索烏龜模塊,首先在我當前的文件夾中尋找turtle.py,而不是從python庫目錄中的turtle.py。 –

-1

請儘量

從tutle imort龜:導入模塊....它會爲你工作

+0

這太沒用了.. – tplive

2

您可以通過安裝python-tk包解決這個問題。

sudo apt-get install python3-tk 
0

我有同樣的問題,但我找到了答案:「重命名它!」它的工作。反正不要使用'進口烏龜'。使用'從烏龜進口*'。