python - PyCharm:在 'xxx' 中找不到引用 'turtle.py'

标签 python reference pycharm

我试着用内置的 lib turtle 画一个五角星。

环境:

  • Windows 10
  • python 3.5
  • PyCharm 社区版 2017.1.5

代码:

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()

turtle 中的所有方法,包括'fillcolor'、'begin_fill'、'forward'、'right' 和'pos' 等,都被PyCharm 警告“在'turtle 中找不到引用'xxx' .py'"和这些方法的自动完成以及警告一起失败。但奇怪的是,脚本能按预期正常正确运行。

我搜索了 SO 的答案,有一些相关的问题,但实际上并不相同:

  1. 在“__init__.py”中找不到引用“xxx”
  2. Unresolved 引用“打印”

以上所有问题的答案都不能解决这个问题。

根据第一批评论和回答,进一步提供以下信息:

  • 我几乎相信我正在为 Python 3.x 使用 turtle,因为我的笔记本电脑中只有一个 turtle.py 文件,位于目录“C:\Python35\Lib”下。顺便说一句,如果我仍然可以在 Python 2.x 中使用 turtle,我该如何检查此信息以及如何在默认目录中更新此内置库?
  • 我几乎相信我没有使用 virtualenv,我的项目的解释器是 Python 3.5.2

(不幸的是,我仍然无法上传图片)

最佳答案

问题在\Lib\turtle.py 中。

魔法变量all定义如下:

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

并且 PyCharm 在扫描包时不会执行此代码。所以 PyCharm 无法定义哪些函数可以在模块级别使用。

所以你可以改变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']

你也可以使用 Turtle 类:

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

关于python - PyCharm:在 'xxx' 中找不到引用 'turtle.py',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172325/

相关文章:

python - 如何在 python 中规范化数据集以进行线性/多元回归

python - 如何从这里获取 span=(2494, 2516) 的第一个数字?

带有省略号_函数的python解释器?

c++ - 为什么我的 EnumWindowProc 不能用 C 编译?

python - codechef 问题 REDONE 中的运行时 NZEC 错误

python - (Python) 阻塞子进程

javascript - 返回引用的 Ecmascript 函数的真实示例?

c++ - & 符号 '&' 参数末尾的运算符

python - Pycharm 与使用相等运算符执行的 None 比较

python - 在 PyCharm 中让 6 和 6.moves 模块自动完成?