在 __init__.py 中找不到引用“xxx”-Python/Pycharm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23248017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:34:54  来源:igfitidea点击:

Cannot find reference 'xxx' in __init__.py - Python / Pycharm

pythonreferencecoding-stylepycharm

提问by pceccon

I have a project in Pycharm organized as follows:

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

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

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

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

Cannot find reference 'xxx' in __init__.py

在 __init__.py 中找不到引用“xxx”

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

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

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)

And I get this warning:

我收到这个警告:

Cannot find reference 'choice' in __init__.py

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

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

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

Thank you in advance.

先感谢您。

采纳答案by Mr. B

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

这是pycharm中的一个错误。PyCharm 似乎希望引用的模块包含在__all__ = []语句中。

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."

为了正确的编码礼仪,您是否应该包含__all__模块中的声明?..这实际上是我们听到年轻的 Spock 在接受测试时回答的问题,他回答说:“这在道德上值得称赞,但在道德上没有义务。”

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.

要绕过它,您可以简单地全局禁用(非常非关键的)(非常有用的)检查,或者为特定的函数或语句抑制它。

To do so:

这样做:

  • put the caret over the erroring text ('choice', from your example above)
  • Bring up the intention menu (alt-enter by default, mine is set to alt-backspace)
  • hit the right arrow to open the submenu, and select the relevant action
  • 将插入符号放在错误文本上(“选择”,来自上面的示例)
  • 调出意图菜单(默认为alt-enter,我的设置为alt-backspace)
  • 点击右箭头打开子菜单,然后选择相关操作

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.

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

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

在您提出问题之后,我知道这已经有点过了,但我希望这会有所帮助(您或其他人)。

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 PyUnresolvedReferenceson the line preceding the statement.

编辑:最初,我认为这是特定于检查的__all__,但看起来它是更通用的“未解析的引用”检查,这可能非常有用。最好使用该功能的语句级禁用,或者通过使用上面提到的菜单,或者通过# noinspection PyUnresolvedReferences在语句前面的行上指定。

回答by Sна?ош?а?

You should first take a look at this. This explains what happens when you import a package. For convenience:

你应该先看看这个。这解释了导入包时会发生什么。为了方便:

The import statement uses the following convention: if a package's __init__.pycode defines a list named __all__, it is taken to be the list of module names that should be imported when from package import *is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don't see a use for importing * from their package.

import 语句使用以下约定:如果包的__init__.py代码定义了一个名为 的列表__all__,则它被认为from package import *是遇到时应导入的模块名称列表。当软件包的新版本发布时,由软件包作者来保持这个列表是最新的。包作者也可能决定不支持它,如果他们没有看到从他们的包中导入 * 的用途。

So PyCharm respects this by showing a warning message, so that the author can decide which of the modules get imported when * from the package is imported. Thus this seems to be useful feature of PyCharm (and in no way can it be called a bug, I presume). You can easily remove this warning by adding the names of the modules to be imported when your package is imported in the __all__variable which is list, like this

因此 PyCharm 通过显示警告消息来尊重这一点,以便作者可以决定在从包中导入 * 时导入哪些模块。因此,这似乎是 PyCharm 的有用功能(我认为它绝不能称为错误)。您可以通过在__all__列表变量中导入包时添加要导入的模块的名称来轻松删除此警告,如下所示

__init__.py

__init__.py

from . import MyModule1, MyModule2, MyModule3
__all__ = [MyModule1, MyModule2, MyModule3]

After you add this, you can ctrl+clickon these module names used in any other part of your project to directly jump to the declaration, which I often find very useful.

添加这个之后,你可以ctrl+click对你项目的任何其他部分使用的这些模块名称直接跳转到声明,我经常发现这非常有用。

回答by Abdullah Noman

I have solved this problem in my pycharm in a bit different way.

我在我的 pycharm 中以稍微不同的方式解决了这个问题。

Go to settings -> Project Interpreter and then click on the base package there.

转到设置 -> 项目解释器,然后单击那里的基本包。

You will see a page like this

你会看到这样的页面

problemGuide

问题指南

After that when your package is installed then you should see the package is colored blue rather than white.

之后,当你的包被安装时,你应该看到包是蓝色而不是白色。

And the unresolved reference is also gone too.

未解决的参考也消失了。

回答by edilio

I know this is old, but Google sent me here so I guess others will come too like me.

我知道这是旧的,但谷歌把我送到这里,所以我猜其他人也会像我一样。

The answer on 2018 is the selected one here: Pycharm: "unresolved reference" error on the IDE when opening a working project

2018年的答案是这里选择的一个:Pycharm:打开工作项目时IDE上的“未解析的引用”错误

Just be aware that you can only add one Content Rootbut you can add several Source Folders. No need to touch __init__.pyfiles.

请注意,您只能添加一个,Content Root但您可以添加多个Source Folders。无需触摸__init__.py文件。

回答by Kai Wang

Did you forget to add the init.py in your package?

你忘记在你的包中添加init.py 了吗?

回答by absin

You can mark source directory as a source root like so:

您可以将源目录标记为源根目录,如下所示:

  • Right-click on source directory
  • Mark Directory As --> Source Root
  • File --> Invalidate Caches / Restart... -> Invalidate and Restart
  • 右键单击源目录
  • 将目录标记为 --> 源根
  • 文件 --> 使缓存无效/重新启动... -> 使缓存无效并重新启动