Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 550
prosseek Avatar asked Jan 20 '14 14:01

prosseek


People also ask

How do I fix unresolved references?

If you still see the unresolved reference error after fixing the problem, try to build your Android application with Command + F9 for Mac or Control + F9 for Windows and Linux. The error should disappear after the build is completed.

Why is import not working in PyCharm?

Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.


2 Answers

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

like image 144
Games Brainiac Avatar answered Oct 16 '22 11:10

Games Brainiac


  1. check for __init__.py file in src folder
  2. add the src folder as a source root
  3. Then make sure to add sources to your PYTHONPATH (see above)
  4. in PyCharm menu select: File --> Invalidate Caches --> Restart
like image 66
Ukr Avatar answered Oct 16 '22 11:10

Ukr