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

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](https://i.stack.imgur.com/9WunC.png)

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

enter image description here

  • Now imports will be resolved:
                  ![enter image description here](https://i.stack.imgur.com/4Z6RD.png)

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](https://i.stack.imgur.com/vMII4.png)

From: stackoverflow.com/q/21236824

Back to homepage or read more recommendations: