I am getting an error while writing the Python program using the turtle module in VS Code:
"Module 'turtle' has no 'TurtleScreen' member"
I am getting an error while writing the Python program using the turtle module in VS Code:
"Module 'turtle' has no 'TurtleScreen' member"
Instead of the functional interface:
import turtle
wn = turtle.TurtleScreen()
Use the object-oriented interface:
from turtle import Screen, Turtle
wn = Turtle()
wn.TurtleScreen()
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:
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.