0

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'
shiva
  • 5,083
  • 5
  • 23
  • 42

3 Answers3

3

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.

cdlane
  • 40,441
  • 5
  • 32
  • 81
0

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

my shell

Derek Eden
  • 4,403
  • 3
  • 18
  • 31
  • I tried to do turtle.pen(), but it again gave me :Traceback (most recent call last): File "", line 1, in turtle.pen() AttributeError: module 'turtle' has no attribute 'pen' – Vladimir Moskalenko Nov 18 '19 at 04:01
  • restart your shell and make sure you aren't defining turtle as something else – Derek Eden Nov 18 '19 at 04:02
  • This answer is incorrect. Confusingly, there are both `Pen` and `pen` functions in Python turtle. One is a synonym for `Turtle`, the other returns or sets the pen attributes of a turtle. You failed to do `turtle.Pen()` in your session above to prove your point, otherwise you would have seen it. Often, beginners name one of their own source files `turtle.py` causing a failure to load Python's turtle.py – cdlane Nov 18 '19 at 05:02
  • When I do `dir(turtle)`, `Pen` is the *second* item (right after `Canvas`) in the returned list of 171 symbols so what point are you trying to make with respect to `dir` and `Pen`? (The `pen` function is the 112th item returned, btw.) – cdlane Nov 18 '19 at 22:16
  • Please see my answer below: you had installed the incorrect python package :) – Guillermo Mosse May 13 '22 at 19:29
0

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

Guillermo Mosse
  • 462
  • 2
  • 14