0

enter image description here

I am getting an error while writing the Python program using the turtle module in VS Code:

"Module 'turtle' has no 'TurtleScreen' member"

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 3
    One of your python files named "turtle.py". You should avoid to name a .py file same as a python module. – Jacky Jan 08 '21 at 06:52
  • @SHAIK EJAZ AHAMED -The reason is that the Python code analysis toolkit "Pylint" does not know whether the "import turtle" in the code refers to the file "turtle" or the Python module "turtle". As people say, please change the name of the file "turtle.py". – Jill Cheng Jan 08 '21 at 09:21
  • 1
    Does this answer your question? [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – Gino Mempin Jan 09 '21 at 05:27
  • Sir, I have changed the file name turtle.py. The problem remains the same. Turtle programs are working when I code them in the command prompt. But I am getting errors while coding in IDE even in IDLE IDE. Please help me. I am new to python. – SHAIK EJAZ AHAMED Jan 09 '21 at 11:27

2 Answers2

0

Instead of the functional interface:

import turtle

wn = turtle.TurtleScreen()

Use the object-oriented interface:

from turtle import Screen, Turtle

wn = Turtle()
wn.TurtleScreen()
Edmund
  • 332
  • 1
  • 9
0

You have 2 problems.

First, you named your file turtle.py. This shadows the actual turtle module, and Python will load your turtle.py instead of the actual turtle built-in module.

You can easily check this by printing the module __file__:

import turtle
print(turtle.__file__)

wn = turtle.TurtleScreen
$ python turtle.Turtl.py 
/path/to/your/turtle.py
...

Because if you rename turtle.py to something else (myturtle.py), it should print out a path to the Python installation folder:

$ python turtle.Turtl.py 
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/turtle.py
...

Second, what you are actually seeing in the VS Code Problems tab, is a pylint error about not finding the module you are importing. It's a common problem with VS Code and pylint. The fix depends on the module you are importing, but generally, you can tell PyLint where to look for modules.

One solution is making sure you activated or selected the correct Python environment in VS Code. I see you are using Python 3.9, so make sure VS Code is using that same Python installation as what you use to run your code:

point to virtual env

Another solution is to add an init-hook option to VS Code's pylintArgs:

{
    "python.linting.pylintArgs": [
        "--init-hook",
        "import sys; sys.path.insert(0, '/usr/local/Cellar/python@3.9/3.9.1_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/')"
    ]
}

Specify the path to the folder containing the module.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135