Unresolved reference issue in PyCharm

Posted on

Question :

Unresolved reference issue in PyCharm

I have a directory structure

├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...

And I can access the network module with sys.path.insert().

import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *

However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?

enter image description here

Asked By: prosseek


Answer #1:

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:

enter image description here

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

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

enter image description here

  • Now imports will be resolved:

enter image description here

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:

enter image description here

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

Answered By: Games Brainiac

Answer #2:

  1. check for __init__.py file in src folder
  2. add the src folder as a source root
  3. Then make sure to add add sources to your PYTHONPATH (see above)
  4. in PyCharm menu select: File –> Invalidate Caches / Restart
Answered By: Ukr

Answer #3:

If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.

ie. Right click on your root folder –> ‘Mark Directory As’ –> Source Root

Answered By: AeroHil

Answer #4:

After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies and re-arrange them.

pycharm prefrence

Answered By: mehdi

Answer #5:

Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.

enter image description here

Answered By: prosseek

Answer #6:

Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it.

Answered By: Ch_y

Answer #7:

Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreter then click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the ‘Install Package’ button.

enter image description here

Answered By: danday74

Answer #8:

After following the accepted answer, doing the following solved it for me:

File ? Settings ? Project <your directory/project> ? Project Dependencies

Chose the directory/project where your file that has unresolved imports resides and check the box to tell Pycharm that that project depends on your other project.

My folder hierarcy is slightly different from the one in the question. Mine is like this

??? MyDirectory  
?     ??? simulate.py  
??? src  
?     ??? networkAlgorithm.py  
?     ??? ...

Telling Pycharm that src depends on MyDirectory solved the issue for me!

Answered By: Benjamin

Leave a Reply

Your email address will not be published. Required fields are marked *