LT. Zh
LT. Zh

Reputation: 43

PyCharm: Cannot find reference 'xxx' in 'turtle.py'

I try to draw a pentacle with built-in lib turtle.

Environment:

code:

import turtle


turtle.fillcolor('red')
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.right(144)
    if abs(turtle.pos()) < 1:
        break
turtle.end_fill()

All of the method in turtle, including the 'fillcolor', 'begin_fill', 'forward', 'right', and 'pos', etc, are warned by PyCharm with "Cannot find reference 'xxx' in 'turtle.py'" and the auto-complete on these methods failed along with the warnings. But it's strange that the script can run normally and correctly as expected.

I've searched SO for answers, and there are a few related questions but not the same, actually:

  1. Cannot find reference 'xxx' in '__init__.py'
  2. Unresolved reference 'print'

All of the answers to the questions above cannot solve this problem.

According to the first batch of comments and answers, some more info provided as below:

(Unfortunatly, I still cannot upload image)

Upvotes: 3

Views: 6080

Answers (1)

Sergey Shashkov
Sergey Shashkov

Reputation: 475

The problem is in \Lib\turtle.py.

Magic variable all is defines as follows:

__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
           _tg_utilities + ['Terminator']) # + _math_functions)

And PyCharm does not execute this code while scanning packages. So PyCharm can not define which functions can be used in module-level.

So you can change turtle.py:

# __all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
#            _tg_utilities + ['Terminator']) # + _math_functions)
__all__ = ['ScrolledCanvas', 'TurtleScreen', 'Screen', 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D', 'back',
           'backward', 'begin_fill', 'begin_poly', 'bk', 'addshape', 'bgcolor', 'bgpic', 'bye', 'clearscreen',
           'colormode', 'delay', 'exitonclick', 'getcanvas', 'getshapes', 'listen', 'mainloop', 'mode', 'numinput',
           'onkey', 'onkeypress', 'onkeyrelease', 'onscreenclick', 'ontimer', 'register_shape', 'resetscreen',
           'screensize', 'setup', 'Terminator', 'setworldcoordinates', 'textinput', 'title', 'tracer', 'turtles',
           'update', 'window_height', 'window_width', 'write_docstringdict', 'done', 'circle', 'clear', 'clearstamp',
           'clearstamps', 'clone', 'color', 'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
           'fillcolor', 'filling', 'forward', 'get_poly', 'getpen', 'getscreen', 'get_shapepoly', 'getturtle', 'goto',
           'heading', 'hideturtle', 'home', 'ht', 'isdown', 'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease',
           'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'right', 'reset',
           'resizemode', 'rt', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setx',
           'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'speed', 'st', 'stamp', 'tilt',
           'tiltangle', 'towards', 'turtlesize', 'undo', 'undobufferentries', 'up', 'width', 'write', 'xcor', 'ycor']

Also you can use Turtle class:

T = turtle.Turtle()
T.fillcolor('red')
etc

Upvotes: 2

Related Questions