简体   繁体   中英

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

I'm trying to use the Pen functionality of turtle module in Python programming, but it gave me this error:

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

Your code is correct:

import turtle
t = turtle.Pen()

Command line session:

% 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()
% 

Make sure you don't have a file of your own lying around named turtle.py as this will interfere with loading Python's own turtle.py library:

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

Which generates the same " AttributeError: module 'turtle' has no attribute 'Pen' " you received.

there is no capital in pen...

it's:

import turtle
turtle.pen()

try doing dir(turtle) to see a list of functions available, in there you will see pen

我的壳

Actual solution: you installed the incorrect package. You should have installed PythonTurtle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM