Unresolved reference issue in PyCharm
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:
Then make sure to add add sources to your
PYTHONPATH
under:Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console
- Now imports will be resolved:
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:
After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart
Solution 2
- check for
__init__.py
file insrc
folder - add the
src
folder as a source root - Then make sure to add sources to your
PYTHONPATH
(see above) - 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.
Solution 5
Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.
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, 2022Comments
-
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?
-
rob mayoff about 10 yearsThis also works if you're using the Python plugin with IntelliJ.
-
Ben Lin about 10 yearsThis 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 about 10 years@BenLin True, however for all other solutions you would have to do the same.
-
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 over 8 yearsYou also need to make sure your content root path is correct. In pycharm 5 you can find this in Preferences -> Project -> Project Structure.
-
SexyBeast almost 8 yearsStrangely, sub-directories are not taken into account. For example, if under
src
, we haveviews
folder inside which I havemyview.py
, I still receive unresolved reference error when tryingfrom src.views.myview import <my_function>
... -
thicolares about 7 yearsJust for the record: if you're using django, the root folder should be that one named
projectName
with themanage.py
inside (not that one with virtual env etc.) -
aimbire over 6 yearsThis very last step was the only thing missing in my solution. The accepted answer might add this to it.
-
Joe over 6 yearsI 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 about 6 yearsThis works great. I just had to add multiple folders as source route. thanks
-
AlexPes over 5 yearsThe solution provided by @robmayoff works, too. Installed Python plugins and the issue has gone away. Thanks
-
y2k-shubham over 5 yearsIn
IntelliJ IDEA 2016+
this option has been relocated; see @AeroHil's answer -
Gandalf Saxe over 5 yearsWhat 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 about 5 yearsThis works perfectly for me. I have installed
pygame
there and not more unresolved reference issue! -
Admin almost 5 yearsSo many complex answers in this discussion. The solution presented here worked for me so easily.
-
theStud54 over 4 yearsDoesnt 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 over 4 yearsHad to combine this + the accepted answer(stackoverflow.com/a/21241988/6448384).
-
Foxocube over 4 yearsWelcome 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 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 almost 4 yearswithout the last step mentioned here, the accepted answer is not working. Accepted answer should add this step as well/
-
H. Arponen almost 4 yearsDamn I just want to mark this answer as a life saver!
-
Stéphane Bruckert over 3 yearswhy was this a problem?
-
Charlie Parker over 2 yearshow does this answer change if my library is installed in editable mode e.g.
pip install -e .
? -
Charlie Parker over 2 yearshow does this answer change if my library is installed in editable mode e.g.
pip install -e .
? -
Charlie Parker over 2 yearswhat does "root folder" mean for you? Like the root of the github repo or the package root e.g. something like
src
? -
Charlie Parker over 2 yearswhat is alt+enter in mac?
-
Charlie Parker over 2 yearshow 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 over 2 yearshow am I suppose to re-arrange them?
-
Charlie Parker over 2 yearsI can't see the python Paths box that your picture shows in 2021.3 version...
-
Charlie Parker over 2 yearshow do I find the
.idea
folder? -
Charlie Parker over 2 yearsAnswering 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 over 2 yearsfor me it seems that deleting the
.idea
folder fixed everything. See stackoverflow.com/a/66701106/1601580 -
Charlie Parker over 2 yearswhy does this work?
-
Charlie Parker over 2 yearsbtw, is recreating the project really necessary?
-
Ouss over 2 yearsRecreating 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 over 2 yearsTo find the
.idea
folder usels -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 tocd
into it thels
as usual. -
Ouss over 2 yearsWhy 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 over 2 yearsAnyway I am happy recreating the .idea folder worked for you…!
-
Charlie Parker over 2 yearsHi 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 thePATH
env variable so I wonder if it's that. -
Charlie Parker over 2 yearsmy
PYTHONPATH
is empty, is that normal? -
Charlie Parker over 2 yearsmy
PYTHONPATH
is empty, is that normal? -
Charlie Parker over 2 yearsmy
PYTHONPATH
is empty, is that normal? -
Charlie Parker over 2 yearsmy
PYTHONPATH
is empty, is that normal? -
Charlie Parker over 2 yearsLet us continue this discussion in chat.
-
TheCodeCache over 2 yearsI'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