Python error: module 'turtle' has no attribute 'Pen'

Python error: module 'turtle' has no attribute 'Pen'

我试图在 Python 编程中使用 turtle 模块的 Pen 功能,但它给了我这个错误:

    [Clang 6.0 (clang-600.0.57)] on darwin
    Type "help", "copyright", "credits" or "license()" for more information.
    >>> import turtle
    >>> t = turtle.Pen()
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        t = turtle.Pen()
    AttributeError: module 'turtle' has no attribute 'Pen'

笔中没有大写...

它是:

import turtle
turtle.pen()

尝试dir(turtle)查看可用功能列表,在那里您会看到笔

您的代码正确:

import turtle
t = turtle.Pen()

命令行会话:

% python3
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
>>> t = turtle.Pen()
>>> exit()
% 

确保您没有自己的名为 turtle.py 的文件,因为这会干扰加载 Python 自己的 turtle.py 库:

% touch turtle.py
% python3
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
>>> t = turtle.Pen()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'turtle' has no attribute 'Pen'
>>> 

生成与您收到的相同的“AttributeError: module 'turtle' has no attribute 'Pen'”。

实际解决方案:您安装了错误的包。您应该已经安装 PythonTurtle