0

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.

2
  • Pycharm's python path wasn't updated with the package - possible reason
    – bigbounty
    Commented Jul 7, 2020 at 16:29
  • Could you please expand the project tree and take a screenshot? Commented Jul 8, 2020 at 7:12

1 Answer 1

-1

Since it works, it seems that the problem is related with the GUI your are using.. I'd check the local paths that your code editor looks into (e.g. PYTHONPATH in Spyder GUI). Hope it solves your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.