在 __init__.py 中找不到引用“xxx" [英] Cannot find reference 'xxx' in __init__.py

查看:26
本文介绍了在 __init__.py 中找不到引用“xxx"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PyCharm 中有一个项目,组织如下:

I have a project in PyCharm organized as follows:

-- Sources
   |--__init__.py
   |--Calculators
      |--__init__.py
      |--Filters.py
   |--Controllers
      |--__init__.py
      |--FiltersController.py
   |--Viewers
      |--__init__.py
      |--DataVisualization.py
   |--Models
      |--__init__.py
      |--Data

我所有的 __init__.py,除了 Sources 正上方的那个都是空白文件.我收到了很多这样的警告:

All of my __init__.py, except for the one right above Sources are blank files. I am receiving a lot of warnings of the kind:

在 __init__.py 中找不到引用xxx"

Cannot find reference 'xxx' in __init__.py

例如,我的 FiltersController.py 有这段代码:

For example, my FiltersController.py has this piece of code:

import numpy.random as npr

bootstrap = npr.choice(image_base.data[max(0, x-2):x+3, max(0, y-2):y+3].flatten(), size=(3, 3), replace=True)

我收到了这个警告:

在 __init__.py 中找不到参考选择"

Cannot find reference 'choice' in __init__.py

我在谷歌上搜索,想知道这是什么意思,我应该怎么做才能在 Python 中正确编码.

I'm googling wondering what does this mean and what should I do to code properly in Python.

推荐答案

这是pycharm的一个bug.PyCharm 似乎期望引用的模块包含在 __all__ = [] 语句中.

This is a bug in pycharm. PyCharm seems to be expecting the referenced module to be included in an __all__ = [] statement.

为了正确的编码礼仪,您是否应该在模块中包含 __all__ 语句?..这实际上是我们听到年轻的 Spock 在接受测试时回答的问题,他回答说:这在道德上值得称赞,但在道德上不是强制性的."

For proper coding etiquette, should you include the __all__ statement from your modules? ..this is actually the question we hear young Spock answering while he was being tested, to which he responded: "It is morally praiseworthy but not morally obligatory."

要绕过它,您可以简单地全局禁用 (非常非关键)(非常有用)检查,或者针对特定功能或语句禁止它.

To get around it, you can simply disable that (extremely non-critical) (highly useful) inspection globally, or suppress it for the specific function or statement.

这样做:

  • 将插入符号放在错误文本上(上面示例中的选择")
  • 调出意图菜单(默认为alt-enter,我的设置为alt-backspace)
  • 点击右箭头打开子菜单,然后选择相关操作

PyCharm 也有类似这样的小错误,但在我看来,它的好处远远超过它的缺点.如果您想尝试其他好的 IDE,还有 Spyder/Spyderlib.

PyCharm has its share of small bugs like this, but in my opinion its benefits far outweigh its drawbacks. If you'd like to try another good IDE, there's also Spyder/Spyderlib.

在您提出问题后,我知道这是相当多的,但我希望这对您(您或其他人)有所帮助.

I know this is quite a bit after you asked your question, but I hope this helps (you, or someone else).

最初,我认为这是特定于检查 __all__ 的,但看起来它是更通用的未解决的引用"检查,这可能非常有用.最好使用该功能的语句级禁用,方法是使用上面提到的菜单,或者在语句之前的行上指定 # noinspection PyUnresolvedReferences.

Edited: Originally, I thought that this was specific to checking __all__, but it looks like it's the more general 'Unresolved References' check, which can be very useful. It's probably best to use statement-level disabling of the feature, either by using the menu as mentioned above, or by specifying # noinspection PyUnresolvedReferences on the line preceding the statement.

这篇关于在 __init__.py 中找不到引用“xxx"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆