[英]Cannot find reference 'xxx' in __init__.py
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 Sources
are blank files.我所有的__init__.py
,除了Sources
上面的那个都是空白文件。 I am receiving a lot of warnings of the kind:我收到了很多这样的警告:
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 中正确编码。
This is a bug in pycharm.这是pycharm中的一个错误。 PyCharm seems to be expecting the referenced module to be included in an __all__ = []
statement. PyCharm 似乎希望引用的模块包含在__all__ = []
语句中。
For proper coding etiquette, should you include the __all__
statement from your modules?为了正确的编码礼节,你应该在你的模块中包含__all__
语句吗? ..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." ..这实际上是我们听到年轻的 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:这样做:
PyCharm has its share of small bugs like this, but in my opinion its benefits far outweigh its drawbacks. PyCharm 也有类似这样的小错误,但在我看来,它的好处远远超过了它的缺点。 If you'd like to try another good IDE, there's also Spyder/Spyderlib.如果您想尝试另一个好的 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.编辑:最初,我认为这是特定于检查__all__
,但看起来它是更通用的“未解析的引用”检查,这可能非常有用。 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.最好使用该功能的语句级禁用,或者使用上面提到的菜单,或者通过在语句之前的行中指定# noinspection PyUnresolvedReferences
。
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__.py
code defines a list named__all__
, it is taken to be the list of module names that should be imported whenfrom package import *
is encountered. import 语句使用以下约定:如果包的__init__.py
代码定义了一个名为__all__
的列表,则它被认为是遇到from package import *
时应导入的模块名称列表。 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.包作者也可能决定不支持它,如果他们没有看到从他们的包中导入 * 的用途。
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.因此 PyCharm 通过显示警告消息来尊重这一点,以便作者可以决定在从包中导入 * 时导入哪些模块。 Thus this seems to be useful feature of PyCharm (and in no way can it be called a bug, I presume).因此,这似乎是 PyCharm 的有用功能(我认为它绝不能称为错误)。 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您可以通过在__all__
变量列表中添加要导入的模块的名称来轻松删除此警告,如下所示
__init__.py __init__.py
from . import MyModule1, MyModule2, MyModule3
__all__ = [MyModule1, MyModule2, MyModule3]
After you add this, you can ctrl+click
on 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
直接跳转到声明,我经常发现这非常有用。
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 Root
but you can add several Source Folders
.请注意,您只能添加一个Content Root
但您可以添加多个Source Folders
。 No need to touch __init__.py
files.无需接触__init__.py
文件。
You can mark source directory as a source root like so:您可以将源目录标记为源根目录,如下所示:
好吧,我真的在这里超越了自己......我试图导入的文件末尾没有.py
扩展名
This problem may also occur if you add several separate projects to one project in PyCharm and mark a different project as a source route.如果您将几个单独的项目添加到 PyCharm 中的一个项目并将不同的项目标记为源路由,也可能会出现此问题。
Check chosen source routes.检查选择的源路由。 It helped me solve the problem with ctrl+click.它帮助我用 ctrl+click 解决了这个问题。
It happened to me in PyCharm when having in the same window 2 attached projects with:它发生在我的 PyCharm 中,在同一个 window 2 个附加项目中:
I had to detach one of the projects to resolve the reference that was pointing to the wrong Python version.我不得不分离其中一个项目来解决指向错误 Python 版本的引用。
This happened to me with the websockets library, but seems to be a more general problem.这发生在我的 websockets 库中,但似乎是一个更普遍的问题。 Example:Cannot find reference 'exceptions' in ' init .py init .py'示例:在“ init .py init .py”中找不到引用“异常”
This worked for me: from websockets import exceptions这对我有用:从 websockets 导入异常
Make sure you didn't by mistake changed the file type of __init__.py
files.确保您没有错误地更改__init__.py
文件的文件类型。 If, for example, you changed their type to "Text" (instead of "Python"), PyCharm won't analyze the file for Python code.例如,如果您将它们的类型更改为“文本”(而不是“Python”),PyCharm 将不会分析文件中的 Python 代码。 In that case, you may notice that the file icon for __init__.py
files is different from other Python files.在这种情况下,您可能会注意到__init__.py
文件的文件图标与其他 Python 文件不同。
To fix, in Settings > Editor > File Types
, in the "Recognized File Types" list click on "Text" and in the "File name patterns" list remove __init__.py
.要修复,请在Settings > Editor > File Types
,在“Recognized File Types”列表中单击“Text”并在“File name patterns”列表中删除__init__.py
。
你忘记在你的包中添加init .py 了吗?
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你会看到这样的页面
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.未解决的参考也消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.