Unresolved reference issue in PyCharm

489,774

Solution 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

Solution 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 sources to your PYTHONPATH (see above)
  4. in PyCharm menu select: File --> Invalidate Caches --> Restart

Solution 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

Solution 4

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

pycharm prefrence

Solution 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

Share:
489,774
prosseek
Author by

prosseek

A software engineer/programmer/researcher/professor who loves everything about software building. Programming Language: C/C++, D, Java/Groovy/Scala, C#, Objective-C, Python, Ruby, Lisp, Prolog, SQL, Smalltalk, Haskell, F#, OCaml, Erlang/Elixir, Forth, Rebol/Red Programming Tools and environments: Emacs, Eclipse, TextMate, JVM, .NET Programming Methodology: Refactoring, Design Patterns, Agile, eXtreme Computer Science: Algorithm, Compiler, Artificial Intelligence

Updated on July 08, 2022

Comments

  • prosseek
    prosseek almost 2 years

    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

  • rob mayoff
    rob mayoff about 10 years
    This also works if you're using the Python plugin with IntelliJ.
  • Ben Lin
    Ben Lin about 10 years
    This solution works. The problem of it is that when another programmer retrieves the code from svn, she has to do the same settings again to get rid of the "unresolved reference" error prompt.
  • Games Brainiac
    Games Brainiac about 10 years
    @BenLin True, however for all other solutions you would have to do the same.
  • Ben Lin
    Ben Lin about 10 years
    @GamesBrainiac, the solution is that the IDE can parse "sys.path.insert(0, "./src")" add that into PYTHONPATH for this specific file, then give proper syntax analysis.
  • lps
    lps over 8 years
    You also need to make sure your content root path is correct. In pycharm 5 you can find this in Preferences -> Project -> Project Structure.
  • SexyBeast
    SexyBeast almost 8 years
    Strangely, sub-directories are not taken into account. For example, if under src, we have views folder inside which I have myview.py, I still receive unresolved reference error when trying from src.views.myview import <my_function>...
  • thicolares
    thicolares about 7 years
    Just for the record: if you're using django, the root folder should be that one named projectName with the manage.py inside (not that one with virtual env etc.)
  • aimbire
    aimbire over 6 years
    This very last step was the only thing missing in my solution. The accepted answer might add this to it.
  • Joe
    Joe over 6 years
    I had to make my "trunk" folder (in SVN) the Source Root. Then it worked for me. When I tried the folder above that; it didn't work for some odd reason.
  • Aqib Mumtaz
    Aqib Mumtaz about 6 years
    This works great. I just had to add multiple folders as source route. thanks
  • AlexPes
    AlexPes over 5 years
    The solution provided by @robmayoff works, too. Installed Python plugins and the issue has gone away. Thanks
  • y2k-shubham
    y2k-shubham over 5 years
    In IntelliJ IDEA 2016+ this option has been relocated; see @AeroHil's answer
  • Gandalf Saxe
    Gandalf Saxe over 5 years
    What is the difference if you do the second step of adding sources to PYTHONPATH or not? Are both step 1 (add src as source content root) and step 2 (add sources to PYTHONPATH) necessary under all circumstances?
  • Michal Przybylowicz
    Michal Przybylowicz about 5 years
    This works perfectly for me. I have installed pygame there and not more unresolved reference issue!
  • Admin
    Admin almost 5 years
    So many complex answers in this discussion. The solution presented here worked for me so easily.
  • theStud54
    theStud54 over 4 years
    Doesnt this have undesirable consequences and defeat one reason to use a virtual environement? That is, if you take that environment somewhere and those global site-packages are installed, then you are in trouble. Thats what i thought.
  • Karuhanga
    Karuhanga over 4 years
    Had to combine this + the accepted answer(stackoverflow.com/a/21241988/6448384).
  • Foxocube
    Foxocube over 4 years
    Welcome to Stack Overflow, and thanks for your answer. Whilst this may be correct, in the future please avoid posting images or links to other sites without context or further details. The link may stop working at some point, making this answer less useful for other users.
  • zaid.mohammed
    zaid.mohammed over 4 years
    @CyberJacob Thank you very much, and thanks a lot for your advice, I've edited the answer by removing the image and adding text instead.
  • santhosh kumar
    santhosh kumar almost 4 years
    without the last step mentioned here, the accepted answer is not working. Accepted answer should add this step as well/
  • H. Arponen
    H. Arponen almost 4 years
    Damn I just want to mark this answer as a life saver!
  • Stéphane Bruckert
    Stéphane Bruckert over 3 years
    why was this a problem?
  • Charlie Parker
    Charlie Parker over 2 years
    how does this answer change if my library is installed in editable mode e.g. pip install -e .?
  • Charlie Parker
    Charlie Parker over 2 years
    how does this answer change if my library is installed in editable mode e.g. pip install -e .?
  • Charlie Parker
    Charlie Parker over 2 years
    what does "root folder" mean for you? Like the root of the github repo or the package root e.g. something like src?
  • Charlie Parker
    Charlie Parker over 2 years
    what is alt+enter in mac?
  • Charlie Parker
    Charlie Parker over 2 years
    how do I check if my python paths are set up correctly? currently, my PYTHONPATH is empty. I am not sure why this wasn't setup by itself correctly. Btw, the link you share is not working.
  • Charlie Parker
    Charlie Parker over 2 years
    how am I suppose to re-arrange them?
  • Charlie Parker
    Charlie Parker over 2 years
    I can't see the python Paths box that your picture shows in 2021.3 version...
  • Charlie Parker
    Charlie Parker over 2 years
    how do I find the .idea folder?
  • Charlie Parker
    Charlie Parker over 2 years
    Answering my own question, I went to the root of my project (not src but say the git repo root folder). Then I did rm -rf .idea. Then restarted pycharm. I really puzzled why this worked...so far. At least the ide is finding the files and the running of the code does too (but that always worked even when running from within pycharm e.g. with the debugger).
  • Charlie Parker
    Charlie Parker over 2 years
    for me it seems that deleting the .idea folder fixed everything. See stackoverflow.com/a/66701106/1601580
  • Charlie Parker
    Charlie Parker over 2 years
    why does this work?
  • Charlie Parker
    Charlie Parker over 2 years
    btw, is recreating the project really necessary?
  • Ouss
    Ouss over 2 years
    Recreating the project happens automatically when starting PyCharm in that folder. That will re-creates the .idea folder that stores project settings, the string search index and other things.
  • Ouss
    Ouss over 2 years
    To find the .idea folder use ls -a to list all files and folders including hidden one (that start with a dot). To see the content of the .idea folder you can use the terminal to cd into it the ls as usual.
  • Ouss
    Ouss over 2 years
    Why does refreshing the cache works for you? Well it could be that the cache is outdated and because of some glitch in PyCharm or some change you made to the project, the cache was not refreshed and functions and classes and packages names lookup table was not refreshed. When running the code, Python interpret your code in real time (almost) and that part has nothing to do with PyCharm. PyCharm is just a fancy text editor in this case.
  • Ouss
    Ouss over 2 years
    Anyway I am happy recreating the .idea folder worked for you…!
  • Charlie Parker
    Charlie Parker over 2 years
    Hi Ouss, thanks for your kind replies. It is sad, it actually doesn't work consistently for some reason :(. Now that I changed file in my project it cannot find the reference to the projects source root src. So I am puzzled why it's not working. It has worked with other laptops out of the box as long as I activate the conda env. Might it be that? I also had some issues earlier with the PATH env variable so I wonder if it's that.
  • Charlie Parker
    Charlie Parker over 2 years
    my PYTHONPATH is empty, is that normal?
  • Charlie Parker
    Charlie Parker over 2 years
    my PYTHONPATH is empty, is that normal?
  • Charlie Parker
    Charlie Parker over 2 years
    my PYTHONPATH is empty, is that normal?
  • Charlie Parker
    Charlie Parker over 2 years
    my PYTHONPATH is empty, is that normal?
  • Charlie Parker
    Charlie Parker over 2 years
  • TheCodeCache
    TheCodeCache over 2 years
    I've just added the parent folder i.e. Directory0 (in this case) and this just worked for me, had been struggling for days on this issue, thank you so much