Pycharm Shows Unresolved References Error for Valid Code

PyCharm shows unresolved references error for valid code

File | Invalidate Caches... and restarting PyCharm helps.

Unresolved reference issue in PyCharm

Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.

This way, you don't have to hard code things into your interpreter's settings:

  • Add src as a source content root:

                           
Sample Image

  • Then make sure to add add sources to your PYTHONPATH under:

    Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console

Sample Image

  • Now imports will be resolved:

                     
Sample Image

This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:

                                 
Sample Image

After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart

Pycharm: unresolved reference error on the IDE when opening a working project

The key is to mark your source directory as a source root. Try the following:

  • In the Project view, right-click on the Python source directory
  • In the dialog menu select Mark Directory As > Source Root

The folder should now appear blue instead of beige, to indicate it is a Python source folder.

You can also configure this in PyCharm preferences by doing the following for a project that is already in PyCharm:

  • In the Mac toolbar, select PyCharm > Preferences
  • In the window that opens, select Project Structure from the menu pane on the left
  • Select your project in the middle pane, if necessary
  • Right-click on your Python source in the right pane and select Sources from the menu dialog

PyCharm unresolved reference for module members

I opened a ticket with JetBrains support, and after a long investigation they found the issue.

Somehow my __init__.py files got associated with text files, and that broke the resolver. I fixed it by going to Settings | Editor | File Types | Text | Registered patterns and deleting __init__.py from the registered patterns.

Thank you JetBrains for the great support!



Related Topics



Leave a reply



Submit