Cannot Find Reference 'Xxx' in _Init_.Py

Cannot find reference 'xxx' in __init__.py

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:

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

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.

TensorFlow Python warning in PyCharm - Cannot find reference __version__ in __init__.py

tf.__version__ is generated dynamically, so the various import hacks are unlikley to work.

A simple hack to work around the warning is using getattr like:

import tensorflow as tf

if getattr(tf, '__version__') < "1.4.0":
....

How to fix Attempted relative import in non-package even with __init__.py

Yes. You're not using it as a package.

python -m pkg.tests.core_test

Cannot find reference 'midi' in '__init__.py'

I found out that Pygame.Midi requires portmidi which is preinstalled with pygame in the form of:

from pygame import pypm
pypm.Initialize()
#needed to initialize pygames compiled version of pyPortMidi

It seems to also be an issue other than the interpreter.

Python __init__ setattr on arguments?

Here you go. Yes this is an ugly evil hack. Yes the object needs a __dict__ variable. But hey, its a neat little one liner!

def __init__(self):
self.__dict__.update(locals())

The constructor can take any type of arguments.

class test(object):
def __init__(self, a, b, foo, bar=5)...

a = test(1,2,3)
dir(a)

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'foo', 'bar', 'self']

It will also include self, but you can easily delete that or make your own update function that ignores self.



Related Topics



Leave a reply



Submit