ChatGPT解决这个技术问题 Extra ChatGPT

Cannot find reference 'xxx' in __init__.py

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.

Just for note, in my case, simply restarting the PyCharm has resolved this warning message, on macOS 10.13.3, PyCharm Professional 2017.3.3.
I just find this happens when I have installed numpy with pip. If I install it separately via Pycharm directly instead, no issues.
None of the proposed solutions worked for me. I ended up using Anaconda which works better with PyCharm. Then hacking the module's __ init __.py to include __all __ = [whatsimported] works.

3
3 revs

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.


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


I agree the warning is not a bug, but the fact you can't ctrl+click on that module could be fixed
@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.
If supporting __all__ is not mandatory and PyCharm produces an error, then yes, this is a bug, because that is not an error. PyCharm could simply issue a warning.
p
per1234

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

Just be aware that you can only add one Content Root but you can add several Source Folders. No need to touch __init__.py files.


this should be the accepted answer! The current one just doesn't work, and this does
added as source root and it fixed the warning
a
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


T
Tonechas

I copied cv2.pyd from the cv2 folder and pasted it inside the site-packages folder and this fixed the problem.

Reference Images:

https://i.stack.imgur.com/cLEAT.png

https://i.stack.imgur.com/abNKb.png


L
Lucas Crostarosa

Well I managed to really outdo myself here.... I didnt have the .py extension at the end of the file I was trying to import


M
Maximus Besman

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.

Check chosen source routes. It helped me solve the problem with ctrl+click.


J
Julienm

It happened to me in PyCharm when having in the same window 2 attached projects with:

one project using a Python interpreter with newer Python version (e.g. 3.6)

another project using a Python interpreter with older Python version (e.g. 2.7)

I had to detach one of the projects to resolve the reference that was pointing to the wrong Python version.


S
Shukrullo

This happened to me with the websockets library, but seems to be a more general problem. Example:Cannot find reference 'exceptions' in 'init.py init.py'

This worked for me: from websockets import exceptions


X
Xavier

Make sure you didn't by mistake changed the file type of __init__.py files. If, for example, you changed their type to "Text" (instead of "Python"), PyCharm won't analyze the file for Python code. In that case, you may notice that the file icon for __init__.py files is different from other Python files.

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.