python cannot find declaration_Cannot find reference 'xxx' in __init__.py - Python / 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

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:

Cannot find reference 'xxx' in init.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)

And I get this warning:

Cannot find reference 'choice' in __init__.py

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

Thank you in advance.

python

reference

coding-style

pycharm

|

this question

edited Apr 23 '14 at 15:29 asked Apr 23 '14 at 14:55

pceccon 1,624 4 16 48      Warnings from

what? An IDE? Python itself does not give warnings like that. –

Daniel Roseman Apr 23 '14 at 15:02      What is giving you that warning? As far as I know, that is not a Python error message. Are you using an IDE or some kind of style checking tool? –

kindall Apr 23 '14 at 15:02      Yes, sorry. I'm using Pycharm. –

pceccon Apr 23 '14 at 15:29

|

2 Answers

2

---Accepted---Accepted---Accepted---

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

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.

To do so:me basic Pygame exercises to try to have something to program. I’ve installed Python 3.4.3 and PyCharm. I also installed the Pygame executable “pygame-1.9.2a0-hg_5974ff8dae3c+.win32-py3.4.msi” from here: https://bitbucket.org/pygame/pyga

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

PyCharm has its

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

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.

|

this answer

edited Sep 1 '14 at 16:41 community wiki

3 revs

Brian Visel

|

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

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

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

|

this answer answered Dec 29 '15 at 15:51

Sнаđошƒаӽ 4,317 7 24 52      I agree the warning is not a bug, but the fact you can't ctrl+click on that module could be fixed –

user110954 Apr 15 '16 at 17:25      @user110954 perhaps PyCharm looks at the

__init__.py file to traceback to the module where a function/variable resides. So without

__all__ having the modules names, it can't traceback to the source. This is my assumption, I may be wrong. –

Sнаđошƒаӽ Apr 15 '16 at 17:30

|

cocosclass HelloWorld(cocos.layer.Layer):def __init__(self): super(HelloWorld, self).__init__() label = cocos.text.Label('Hello, World!', font_name = 'Times New Roman', f

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值