The Python Oracle

Unresolved reference issue in PyCharm

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping

--

Chapters
00:00 Unresolved Reference Issue In Pycharm
00:24 Answer 1 Score 15
00:38 Accepted Answer Score 1159
01:33 Answer 3 Score 21
01:46 Answer 4 Score 81
02:08 Thank you

--

Full question
https://stackoverflow.com/questions/2123...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #ide #pycharm

#avk47



ACCEPTED ANSWER

Score 1159


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




ANSWER 2

Score 81


  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



ANSWER 3

Score 21


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

pycharm prefrence




ANSWER 4

Score 15


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

enter image description here