3

I just started with turtle and upon writing a basic code which is as follows:

import turtle

srijan_turtle = turtle.Turtle()
srijan_turtle.forward(100)
turtle.done()

I got this error:
srijan_turtle = turtle.Turtle() AttributeError: partially initialized module 'turtle' has no attribute 'Turtle' (most likely due to a circular import). Did you mean: 'turtle'?

NOTE: The file was saved as turtle.py

Upon searching for a solution regarding the same, I found this answer over here:
AttributeError: partially initialized module 'turtle' has no attribute 'Turtle' (most likely due to a circular import)

One of the answers there said that:
Simply changing the file name to anything other than, turtle.py would rectify the issue. And it did.

But I did not understand why? Could anyone please explain it to me?

srijan
  • 39
  • 2
  • "But I did not understand why? Could anyone please explain it to me?" For the reason that is given in that answer: "When you import `turtle`, it imports your file, not the turtle library. " – Karl Knechtel May 26 '22 at 03:13

2 Answers2

3

import turtle looks for a module called turtle in order to import it. One of the first places Python's importing mechanism looks is the current directory, in a file called turtle.py, so your file imports itself (instead of importing the actual Turtle library that provides turtle functions).

To figure out where the file is located, you can print turtle.__file__:

# without a turtle.py present in the local dir
>>> import turtle
>>> print(turtle.__file__)
C:\Python310\lib\turtle.py

It should logically follow that you can do the same intentionally - you can create a Python file with functions you want to reuse, and import it by its name from another file in the same directory.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Where is the actual turtle library located? – srijan May 26 '22 at 02:48
  • 2
    Without `turtle.py` obscuring it, execute `import turtle; print(turtle.__file__)` to see where it was sourced from. – Amadan May 26 '22 at 02:49
  • 1
    @srijan It depends on your operating system and configuration (as well as whether the package was built-in, or installed with a package manager). I'll edit the answer to add an example. – nanofarad May 26 '22 at 02:51
  • 1
    Thanks so much! I just tried it and finally understood it. When I kept the file name as turtle.py and ran the command that you gave in your answer, it gave me the location of my file and not the location of the turtle library. And that's why, the program wasn't running. Because it didn't have access to it's library. Thankyou, once again! – srijan May 26 '22 at 03:05
1

You are importing a module named turtle inside a module named turtle. It believes it should import itself, and does not find the actual turtle library.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • did you mean, a module named turtle inside a _file_ named turtle? Or is the file that I created is a module in itself? I don't get it. – srijan May 26 '22 at 02:45
  • 1
    Very simplified, but a module is either a file, or a directory that has a `__init__.py` file. So your `turtle.py` is itself a module called `turtle`. – Amadan May 26 '22 at 02:47
  • Is there a way that I can name my file _turtle.py_ and not get this error? Or is this like something that must be followed? – srijan May 26 '22 at 02:52
  • 1
    At this time, both the `turtle` library module and your own `turtle` module are top-level modules. You could hide your own `turtle` file inside another module (e.g. `mycode`), in which case you would need to invoke it as `python -m mycode.turtle`. – Amadan May 26 '22 at 02:57
  • I see. I'll try this out! Thankyou so much for helping! I finally understand it! – srijan May 26 '22 at 03:08
  • 2
    "did you mean, a module named turtle inside a file named turtle? Or is the file that I created is a module in itself? I don't get it. " In Python, loading a Python source file (with a `.py` extension) creates a module. The module represents the code in the file. We tend to talk about the module and the source file interchangably. When you `import turtle`, Python looks *somewhere* for a file named `turtle.py`, reads the code, and uses it to create a module that is named `turtle`. The file name determines the module name. – Karl Knechtel May 26 '22 at 03:16