下面的代码完美运行,但是,PyCharm 抱怨语法错误forward(100)
#!/usr/bin/python
from turtle import *
forward(100)
done()
由于turtle
是一个标准库,我认为我不需要做额外的配置,对吗?
下面的代码完美运行,但是,PyCharm 抱怨语法错误forward(100)
#!/usr/bin/python
from turtle import *
forward(100)
done()
由于turtle
是一个标准库,我认为我不需要做额外的配置,对吗?
通过在模块中指定源代码中的相关部分,该forward()
函数可用于导入:__all__
turtle
_tg_turtle_functions = [..., 'forward', ...]
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
_tg_utilities + _math_functions)
目前,pycharm 看不到模块__all__
列表中列出的对象,因此将它们标记为unresolved reference
. 它的 bugtracker 中有一个未解决的问题:
从方法中创建函数:更新 __all__ 如果存在星号导入使用
另请参阅:有人可以用 Python 解释 __all__ 吗?
仅供参考,您可以添加noinspection
注释以告诉 Pycharm 不要将其标记为未解析的引用:
from turtle import *
#noinspection PyUnresolvedReferences
forward(100)
done()
或者,禁用特定范围的检查。
import turtle
turtle.forward(100)
turtle.done()
另一种解决方案是显式创建一个Turtle
对象。然后自动完成就可以正常工作,并且一切都更加明确。
import turtle
t = turtle.Turtle()
t.left(100)
t.forward(100)
turtle.done()
或者
from turtle import Turtle
t = Turtle()