-1

When I use turtle no matter what I get this error message:

File "[REDACTED FOR SECURITY]", line 1, in <module>
    from turtle import *
  File "[REDACTED FOR SECURITY]", line 5, in <module>
    turtle.bgcolor('black')
AttributeError: partially initialized module 'turtle' has no attribute 'bgcolor' (most likely due to a circular import)

I wasn't even using turtle.bgcolor().

Here is my code, to draw a fern:

from turtle import *
import random

pen = turtle.Turtle()
pen.speed(15)
pen.color("blue")
pen.penup()

x = 0
y = 0
for n in range(110000):
    pen.goto(65 * x, 37 * y - 252)  # 57 is to scale the fern and -275 is to start the drawing from the bottom.
    pen.pendown()
    pen.dot()
    pen.penup()
    r = random.random()  # to get probability
    r = r * 100
    xn = x
    yn = y
    if r < 1:  # elif ladder based on the probability
        x = 0
        y = 0.16 * yn
    elif r < 86:
        x = 0.85 * xn + 0.04 * yn
        y = -0.04 * xn + 0.85 * yn + 1.6
    elif r < 93:
        x = 0.20 * xn - 0.26 * yn
        y = 0.23 * xn + 0.22 * yn + 1.6
    else:
        x = -0.15 * xn + 0.28 * yn
        y = 0.26 * xn + 0.24 * yn + 0.44
aneroid
  • 12,983
  • 3
  • 36
  • 66
  • 1
    you set `[REDACTED FOR SECURITY]` but there can be more important information for this problem - did you save code in file `turtle.py` ? If you have file with name `turtle.py` then it tries to import your file and it can't find `bgcolor` in your file. Rename your file. – furas Jan 29 '21 at 23:42
  • 2
    if you want to use `turtle.Turtle()` then you need `import turtle` instead of `from turtle import *`. Besides `import *` is not preferred - see [PEP 8 -- Style Guide for Python Code](https://www.python.org/devpeps/pep-0008/) – furas Jan 29 '21 at 23:44

2 Answers2

0

As pointed out in this answer, you need to import the turtle module itself, instead of only importing all of its attributes:

import turtle

Thee reason is because you have

pen = turtle.Turtle()

where if you didn't do import turtle, the turtle in that line wouldn't be defined.


One thing I want to address is that you don't need the turtle pen to be down in order for the turtle.dot() method to work, hence reducing the

    pen.pendown()
    pen.dot()
    pen.penup()

to

    pen.dot()

will effectively improve your program's efficiency.

Red
  • 26,798
  • 7
  • 36
  • 58
0

The comment by @furas about your import statement is on the mark (+1). Based on other calls in your code, e.g. speed(15) and pendown() before dot(), it's clear you've not worked with turtle before and/or read its documentation.

If I were writing this for pure speed, I'd do something more like:

from turtle import Screen, Turtle
from random import random

x, y = 0, 0

def draw():
    global x, y

    turtle.goto(65 * x, 37 * y - 252)
    turtle.dot()

    r = random() * 100  # to get probability

    xn, yn = x, y

    if r < 1:  # elif ladder based on the probability
        x = 0
        y = 0.16 * yn
    elif r < 86:
        x = 0.85 * xn + 0.04 * yn
        y = -0.04 * xn + 0.85 * yn + 1.6
    elif r < 93:
        x = 0.20 * xn - 0.26 * yn
        y = 0.23 * xn + 0.22 * yn + 1.6
    else:
        x = -0.15 * xn + 0.28 * yn
        y = 0.26 * xn + 0.24 * yn + 0.44

    screen.update()
    screen.ontimer(draw)

screen = Screen()
screen.tracer(False)

turtle = Turtle()
turtle.color('blue')
turtle.penup()

draw()

screen.exitonclick()

This will run forever until you click on the window, whereupon it will stop and disappear.

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Every time I import turtle I get the same message error. If i just do import turtle, I get that message error. – user14545830 Feb 12 '21 at 02:26
  • @user14545830, one error that leads to a message like the one you describe is if you name one of your own source files **turtle.py** -- Python then confuses it with the **turtle.py** library and you get errors. Something to look for. – cdlane Feb 15 '21 at 06:44