I am running PyCharm 2019.2 on Ubuntu, with a project using Python 2. I have the following directory structure for my code:
Code
|____Common
|____utils.py
|____Testing
|____test.py
In test.py
, I want to import utils.py
. So, I simply have the line from Common import utils
at the top of test.py
.
This gave me the following error:
No module named Common
After reading up on this problem, I did two things. First, I added my Code directory to the Sources Root. Second, I added an __init__.py
file to the Common directory. After doing this, the error went away.
So, it seems that Python 2 requires you to explicitly mark a directory as a package (using __init__.py
) if you want to import one of its files as a module
But my question is as follows. In PyCharm, there is a red line underneath utils
in the line from Common import utils
. And when hovering over this, the message is: Unresolved reference 'utils'
. This happens despite the fact that my code can run fine.
Why do I receive this error message telling me that there is an unresolved reference, when PyCharm is in fact able to find this reference?
UPDATE:
I have also tried running File -> Invalidate Caches / Restart
. But upon restarting, there is still the red underline.