본문 바로가기
Python/Python FAQ

Python PyCharm에서 미해결된 참조 문제, Unresolved reference issue in PyCharm

by 베타코드 2023. 10. 7.
반응형

질문


저는 디렉토리 구조를 가지고 있습니다.

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

그리고 sys.path.insert()를 사용하여 네트워크 모듈에 액세스할 수 있습니다.

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

하지만, pycharm은 해당 모듈에 액세스할 수 없다고 불평합니다. pycharm에게 참조를 해결하는 방법을 가르칠 수 있을까요?

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

  • 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

반응형

댓글