Can't import turtle module in Python 2.x and Python 3.x

半城伤御伤魂 提交于 2019-11-27 18:12:09

问题


I want to play with turtle module in Python. But when i do import turtle module, i've the following error:

$ 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'

and for 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'

I working under Kubuntu Linux 12.10. I've played with Tkinter gui. There is no problem. What happen with turtle module?


回答1:


You've called a script turtle.py, which is shadowing the turtle module in the standard library. Rename it.




回答2:


You can fix this problem by installing the python-tk package.

sudo apt-get install python3-tk



回答3:


I had the same problem but I found answer: "Rename it!" and it worked. anyways don't use 'import turtle'. Use 'from turtle import *'.




回答4:


I was unable to locate any file with turtle.py so I uninstalled Python and reinstalled a 64 bit version from Python.org. This time the program ran after I typed the following two lines of code into the terminal (black screen).

import turtle

shelly=turtle.Turtle()

Of course your turtle can be called other names and not necessarily shelly.




回答5:


before installing try using:

from turtle import *

g=Turtle()

begin_fill()

g.forward(100)

check out if this working or not(there is space between import and asterick sign)




回答6:


Make sure also that you run:

which python Then set the top of your file with that.

#!/usr/bin/python
import Tkinter
from turtle import *

Or if you don't have the location correct, you need to rename the file with .py Then execute with python filename.py




回答7:


please try

from tutle imort Turtle: to import the module....it will work for you



来源:https://stackoverflow.com/questions/17530140/cant-import-turtle-module-in-python-2-x-and-python-3-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!