PyCharm shows unresolved references error for valid code
Solution 1
File | Invalidate Caches... and restarting PyCharm helps.
Solution 2
Dmitry's response didn't work for me.
I got mine working by going to Project Interpreters, Selecting the "Paths" tab, and hitting the refresh button in that submenu. It auto-populated with something called "python-skeletons".
edit: screenshot using PyCharm 3.4.1 (it's quite well hidden)
Solution 3
There are many solutions to this, some more convenient than others, and they don't always work.
Here's all you can try, going from 'quick' to 'annoying':
- Do
File
->Invalidate Caches / Restart
and restart PyCharm.- You could also do this after any of the below methods, just to be sure.
- First, check which interpreter you're running:
Run
->Edit Configurations
->Configuration
->Python Interpreter
. - Refresh the paths of your interpreter:
-
File
->Settings
-
Project: [name]
->Project Interpreter
-> 'Project Interpreter': Gear icon ->More...
- Click the 'Show paths' button (bottom one)
- Click the 'Refresh' button (bottom one)
-
- Remove the interpreter and add it again:
-
File
->Settings
-
Project: [name]
->Project Interpreter
-> 'Project Interpreter': Gear icon ->More...
- Click the 'Remove' button
- Click the 'Add' button and re-add your interpeter
-
- Delete your project preferences
- Delete your project's
.idea
folder - Close and re-open PyCharm
- Open your project from scratch
- Delete your project's
- Delete your PyCharm user preferences (but back them up first).
-
~/.PyCharm50
on Mac -
%homepath%/.PyCharm50
on Windows
-
- Switch to another interpreter, then back again to the one you want.
- Create a new virtual environment, and switch to that environments' interpreter.
- Create a new virtual environment in a new location -- outside of your project folder -- and switch to that environment's interpreter.
- Switch to another interpreter altogether; don't switch back.
If you are using Docker, take note:
- Make sure you are using
pip3
notpip
, especially with remote docker and docker-compose interpreters. - Avoid influencing
PYTHONPATH
. More info here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000058690-Module-not-found-in-PyCharm-but-externally-in-Python .
If the above did not work for you, but you did find another trick, then please leave a comment.
Solution 4
In my case it was the directories structure. My project looks like this:
+---dir_A
+---dir_B
+app
|
\-run.py
So right click on dir_b > "mark directory as" > "project root"
Solution 5
You have to mark your root directory as:
SOURCE ROOT
(red),
and your applications:
EXCLUDED ROOT
(blue).
Then the unresolved reference will disappear. If you use PyChram pro it do this for you automatically.
James McCalden
Lead Software Engineer in Test Specialist in Automation Tools and Framework development, Performance Testing and NFT.
Updated on February 15, 2022Comments
-
James McCalden over 2 years
I am using PyCharm to work on a project. The project is opened and configured with an interpreter, and can run successfully. The remote interpreter paths are mapped properly. This seems to be the correct configuration, but PyCharm is highlighting my valid code with "unresolved reference" errors, even for built-in Python functions. Why don't these seem to be detected, even though the code runs? Is there any way to get PyCharm to recognize these correctly?
This specific instance of the problem is with a remote interpreter, but the problem appears on local interpreters as well.
-
Mike Johnston over 10 yearsThis helped for my environment. I'm running OSX on my local dev box and Ubuntu on the remote server. The remote interpreter setup didn't seem to properly auto configure. Hitting the refresh button did the trick - mostly: a minor difference between environments (2.7.2 locally and 2.7.3 remotely)seems to have caused breakage with SQLAlchemy.
-
illagrenan about 10 yearsYour tip solved my problem with unresolved built-ins using PyCharm 3.4 EAP.
-
Baskaya almost 10 yearsWhere is "Python Interpreters". Do you mean "Project Interpreters"? I am using Linux and I couldn't find Paths tab either.
-
Daniel Bushman almost 10 yearsIn PyCharm 3.4 on OSX, it's an icon which looks like a little folder tree, and doesn't say "Paths".
-
feeeper over 9 yearsCan anybody post a screenshot with that setting?
-
raphaeltm over 9 yearsFor anybody still confused (in PyCharm 3.4.1):
Settings > Project Settings > Project Interpreter > Project Interpreters (gear button > more) > --Select the interpreter-- > Interpreter Paths (Directory Tree button on the right) > Reload List of Paths (Blue refresh-like button)
-
dtheodor over 9 yearsThere is no such refresh button in PyCharm 4, only the plus and minus signs.
-
Sarah Messer over 9 yearsOn PyCharm4, the path to the setting is File > Settings > Project: [project name] > [gear symbol to the right of the Project Interpreter dropdown] > More... (Project Interpreters) > Show paths for selected Interpreter (the bottom icon, the folder-and-tree icon) > Reload list of paths (bottom lue icon). It is visually similar to the PyCharm 3 screenshot above.
-
Jonathan Geisler over 9 years@Sarah In PyCharm 4.0.4, in the "Interpreter Paths" window, I don't have the "reload list of paths" button (bottom blue refresh icon). Also, I don't see any paths in the list and the plus and minus buttons are disabled.
-
James Fletcher over 9 yearsSwitching to the local Python 2 interpreter and then back again to your remote Python 3 interpreter fixed this for me too.
-
danius over 9 yearsThis is the only way working on PyCharm 4.0.x in my case. The fact is if something got screwed up and it does not refresh (sometimes it even shows same interpreter repeated several times in the list) you'll have to delete the files manually... I've even had to remove .idea folders once.
-
Chris Withers almost 9 yearsThis is so heavyweight. Is there no way to get the old "refresh path" option back in PyCharm 4.5?
-
ecoe over 8 yearsSimilar to this solution, from a previous install of Pycharm I had old references to interpreters that no longer existed (in parentheses they said "invalid"). I deleted all interpreters by following the picture in the above solution and reloaded the right one.
-
Josh over 8 yearsThis works on PyCharm 5 professional as well. None of the other solutions in this post have worked for me. I went into
Settings -> "Project Interpreters"
, clicked the gear, and then"More..."
. Chose the default python interpreter (/usr/bin/python2.7
in my case), hit"Ok"
and"Apply"
. Then went back in and chose the actual virtualenv I wanted to use."Ok"
and"Apply"
again, and it's fixed. -
florisla over 8 yearsThe question was not that PyCharm doesn't recognize project code. It doesn't recognize the standard Python built-in methods.
-
Alex about 7 yearsI've been having this issue for as long as I've been using PyCharm and they still can't reliably fix it. Good thing that you've covered most of the work around that one way or another ends up fixing the problem. Funny how I come back to this answer from time to time to figure out what work around I forgot to try.
-
Mark almost 7 yearsI worked my way down this list and removing/re-adding the interpreter worked for me. Thanks!
-
Bob Stein over 6 yearsSimilarly
FileNotFoundError
is a Python-3-only thing. After I set the correct interpreter in both File-Settings-Project-Interpreter and Run-Edit-Configurations, one of those made the error go away. -
Eduardo Pignatelli almost 6 yearsIt is really annoying that such a IDE doesn't take care of such a simple operation.
-
Mike - SMT over 5 yearsYou can quickly get to the project interpreter screen with
ctrl+alt+s
. Something to add to your answer for ease of use for readers. -
Jose almost 5 yearsI have pycharm pro and it did not do this for me but after I manually marked the folders correctly it worked
-
Aseem almost 5 yearsIt works momentarily. After few minutes I get the same warning again
-
Sameen over 4 yearsSolved my issue on PyCharm 2019.3.3 on MacOS
-
predmod about 4 yearsYes, that is the right answer and not some nuclear option where the side effect helps for a moment but does not address how the jetbrains tooling thinks ("kill the cache", right ... and when it gets rebuilt ? "kill it again!"). The title, which was exactly problem in of my projects, where the project root is not necessarily the sources root and imports fail to understand that. Here is information straight from the horses mouth jetbrains.com/help/pycharm/content-root.html "PyCharm uses the source roots as the starting point for resolving imports" !
-
predmod about 4 yearsThe accepted answer is heavyweight and very likely does not address the crux of the problem. Here is information straight from the horses mouth: JetBrains - PyCharm Content Root "PyCharm uses the source roots as the starting point for resolving imports" I had the issue exactly as in the title because in some projects I need to use a file tree where sources are in a subfolder, PyCharm is then confused about imports. Killing cache might help for a moment but is not the right approach. Mark the sources directory as root and all is good.
-
Stephen almost 4 yearsWarning: You might think that this solves your problem even though it doesn't. You can see at the bottom when Pycharm reopens that it's simply rebuilding the cache. During this time, some of the code analysis is turned off. So if this hides your warning messages, it might not actually be solving the problem! That is why @Aseem experienced it as only momentarily resolving the issue. Once the rebuild completes, the error returns. The true fix is likely to be more like what predmod is suggesting.
-
Floating Sunfish over 3 yearsApparently, this was my issue. Thanks!
-
deed02392 over 3 yearsInvalidating cache and restarting didn't work for me, but this did!
-
Copy and Paste over 3 yearsTwo more tips: 1) If applicable make sure you are using
pip3
notpip
, especially with remote docker and docker-compose Interpreters. 2) If you are using docker-compose and/or docker playing around withPYTHONPATH
can cause these types of issues. More info here: intellij-support.jetbrains.com/hc/en-us/community/posts/… -
The Unknown Dev about 3 yearsWorked for me in version 2021.1, after trying all the other tips like reloading skeletons, etc.
-
Denis Barmenkov about 3 yearsIt will also clean local history.
-
kmiklas almost 3 yearsI think I love you
-
Salvador Nava almost 3 yearsSimply and easy fix. No other solution worked for me, but this! Thank you so much!
-
Kerwin Sneijders over 2 yearsI tried everything above and for me creating a new virtual environment worked only when creating it at an entirely different location. I had a
venv
folder in my project but it kept showing as 'unresolved reference' (even though it executed properly). I created a new venv outside of my project folder and then it worked. No idea why. -
Behrouz.M over 2 yearsjust marking the odoo dir as `source root' did the trick
-
Den Kasyanov over 2 yearsThis is the closest answer to what worked for me with Pycharm 2021.3.2. I deleted JetBrains directory from
~/Library/Caches/
and one or more files in~/Library/Preferences
as there were no JetBrains-related directories. In my case imports were unresolved with the docker-compose intertpreter. But everything worked with the Docker interpreter using the service started from exactly the same docker-compose file. Crazy. -
deps_stats about 2 years@predmod How to mark the sources directory as root? Could you please write an answer on how to do this in general? In my case I am using a specific package called otree. The unresolved references come from that package only. I guess that if I use that directory as root it will get solved. Invalidating caches uninstalls the packages and all its dependencies. It's not a solution as you said.
-
scphantm about 2 yearsmoving the venv folder out of the project folder worked for me too.
-
chaostheory about 2 yearsThis fixes the problem, but it introduces a new one: you can no longer scan for problems