Unresolved Reference Issue in Pycharm

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 shows unresolved references error for valid code

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

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!

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 on import?

There are two things at play, here:

Like Eskapp says in the comment, PyCharm is configured to a different Project Root than you seem to expect, as evidenced by it marking your imports as unresolved references. You didn't provide any hints to your project structure, but its likely that your Project Root is set to the parent directory of your Python module (or even further up the hierarchy). PyCharm is looking for ${PROJECTROOT}/oath2_helper and isn't finding it, because Project Root is set to the wrong directory.

You then try to fix this problem by changing to relative imports, which seems like a logical solution. It looks like you're directly calling the module in which those imports are written (i.e. python myapp.py). When you call a module in this way, it drops information about the package structure, and no longer has any information about where other modules are relatively located. See https://stackoverflow.com/a/73149/11034626

I keep getting the Unresolved reference error when I use kivy properties in PyCharm

There are many solutions to this, some more convenient than others, and they don't always work.

File > Settings > Project > Project Interpreter 
> Select "No interpreter" in the "Project interpreter" list
> Apply > Set your python interpreter again > Click Apply

Here's all you can try, going from 'quick' to 'annoying':

Do File -> Invalidate Caches / Restart and restart PyCharm.
You could also do this after any of the below methods, just to be sure.
First, check which interpreter you're running: Run -> Edit Configurations -> Configuration -> Python Interpreter.

Refresh the paths of your interpreter:
File -> Settings
Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More...
Click the 'Show paths' button (bottom one)
Click the 'Refresh' button (bottom one)

Remove the interpreter and add it again:
File -> Settings
Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More...
Click the 'Remove' button
Click the 'Add' button and re-add your interpeter

Delete your project preferences
Delete your project's .idea folder
Close and re-open PyCharm
Open your project from scratch

Delete your PyCharm user preferences (but back them up first).
~/.PyCharm50 on Mac
%homepath%/.PyCharm50 on Windows
Switch to another interpreter, then back again to the one you want.
Create a new virtual environment, and switch to that environments' interpreter.
Switch to another interpreter altogether, don't switch back.

If you are using Docker, take note:

Make sure you are using pip3 not pip, especially with remote docker and docker-compose interpreters.
Avoid influencing PYTHONPATH. More info here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000058690-Module-not-found-in-PyCharm-but-externally-in-Python



Related Topics



Leave a reply



Submit