无法在 Python 2.x 和 Python 3.x 中导入海龟模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17530140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't import turtle module in Python 2.x and Python 3.x
提问by Mind Mixer
I want to play with turtlemodule in Python. But when i do import turtle module, i've the following error:
我想在 Python 中使用乌龟模块。但是当我导入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'
and for Python 3.x:
对于 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?
我在 Kubuntu Linux 12.10 下工作。我玩过 Tkinter gui。没有问题。乌龟模块会发生什么?
采纳答案by Ignacio Vazquez-Abrams
You've called a script turtle.py
, which is shadowing the turtle
module in the standard library. Rename it.
您调用了一个 script turtle.py
,它隐藏turtle
了标准库中的模块。重命名它。
回答by tsegay
please try
请尝试
from tutle imort Turtle: to import the module....it will work for you
from tutle imort Turtle: 导入模块......它会为你工作
回答by Behzad Ghotbifar
You can fix this problem by installing the python-tk
package.
您可以通过安装python-tk
软件包来解决此问题。
sudo apt-get install python3-tk
回答by Ben
I had the same problem but I found answer: "Rename it!" and it worked. anyways don't use 'import turtle'. Use 'from turtle import *'.
我遇到了同样的问题,但我找到了答案:“重命名!” 它奏效了。无论如何不要使用'进口龟'。使用'从海龟进口*'。
回答by ICU
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).
我无法找到任何文件,turtle.py
所以我卸载了 Python 并从 Python.org 重新安装了 64 位版本。这次程序在我在终端中输入以下两行代码后运行(黑屏)。
import turtle
shelly=turtle.Turtle()
Of course your turtle can be called other names and not necessarily shelly
.
当然,你的乌龟可以叫其他名字,不一定shelly
。
回答by Pawan Singh
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)
检查这是否有效(导入和星号之间有空格)
回答by Jonni2016aa
Make sure also that you run:
还要确保您运行:
which python Then set the top of your file with that.
which python 然后用它设置文件的顶部。
#!/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
或者如果你没有正确的位置,你需要用 .py 重命名文件然后用 python filename.py 执行