-1

My code

import turtle 
turtle.forward(15)

It shows the error : partially initialized module 'turtle' has no attribute 'forward' (most likely due to a circular import)

user16017842
  • 1
  • 1
  • 2

2 Answers2

2

Oh yeah I savved my file by turtle.py

-- comment by OP

Don't do that! Your code will not import the Python turtle.py library but rather assume your code is that library. And all the functions and methods, like forward() will be missing.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
cdlane
  • 40,441
  • 5
  • 32
  • 81
1

[Edit: I misinterpreted the problem. The answer below was given on the assumption that the turtle-library only works in an object oriented way.]

The problem with your code is that you to not create an instance of the turtle class before using is. Importing a library with "import turtle" basically only is like telling you program that a library with that name exists. To use the functionality of the imported library, you first have to generate a turtle-object (which you can also name turtle, but I'm using my_turtle to clarify the difference).

import turtle
my_turtle = turtle.Turtle
my_turtle.forward(20)

So you import the turtle library, which contains a class named Turtle, and with "my_turtle = turtle.Turtle", you create an instance of that class, which is a Turtle. Mind that you also have to set a screen on which your turtle draws, using

screen = turtle.Screen
screen.exitonclick()

Additionally, you should refer to the documentation of the turtle library. Turtle is very well documented in a way that even new programmers can easily learn the basic concepts by reading the documentation: https://docs.python.org/3/library/turtle.html

Permafrost84
  • 34
  • 1
  • 2
  • 9
  • 1
    The premise of your answer is incorrect. The OP's code *as written* is a valid turtle program. It will open a window, move a turtle forward 15 pixels, and then, depending on the environment, close the window or leave it open. Turtle has *both* functional and object-oriented interfaces. This is the functional one. – cdlane May 28 '21 at 17:56
  • Oh, thanks for the clarification, I clearly missed that. I will edit my answer accordingly. – Permafrost84 May 28 '21 at 19:42