I have a project in Pycharm organized as follows:
-- Sources
|--__init__.py
|--Calculators
|--__init__.py
|--Filters.py
|--Controllers
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.
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. 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 have solved this problem in my pycharm in a bit different way.
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.
You can mark source directory as a source root like so:
Did you forget to add the init.py in your package?
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:
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.