There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea
issue causing the IDE to show error while the code still runs correctly. Solution:
- close the project and quick PyCharm
- delete the
.idea
folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
- start PyCharm and recreate the project
imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
- Find the folder that relative imports require in the project explorer
- right click and mark it as "Source Root"
Editor not marking init.py as Python
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py
files not to be python files, and thus ignores them during code analysis. To fix this:
- Open PyCharm settings
- Navigate to Editor -> File Types
- Find Python and add
__init__.py
to the list of python files or find Text and delete __init__.py
from the list of text files