PyCharm shows unresolved references error for valid code

228,329

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)

enter image description here

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 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:

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.

enter image description here

Share:
228,329
James McCalden
Author by

James McCalden

Lead Software Engineer in Test Specialist in Automation Tools and Framework development, Performance Testing and NFT.

Updated on February 15, 2022

Comments

  • James McCalden
    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
    Mike Johnston over 10 years
    This 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
    illagrenan about 10 years
    Your tip solved my problem with unresolved built-ins using PyCharm 3.4 EAP.
  • Baskaya
    Baskaya almost 10 years
    Where is "Python Interpreters". Do you mean "Project Interpreters"? I am using Linux and I couldn't find Paths tab either.
  • Daniel Bushman
    Daniel Bushman almost 10 years
    In PyCharm 3.4 on OSX, it's an icon which looks like a little folder tree, and doesn't say "Paths".
  • feeeper
    feeeper over 9 years
    Can anybody post a screenshot with that setting?
  • raphaeltm
    raphaeltm over 9 years
    For 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
    dtheodor over 9 years
    There is no such refresh button in PyCharm 4, only the plus and minus signs.
  • Sarah Messer
    Sarah Messer over 9 years
    On 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
    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
    James Fletcher over 9 years
    Switching to the local Python 2 interpreter and then back again to your remote Python 3 interpreter fixed this for me too.
  • danius
    danius over 9 years
    This 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
    Chris Withers almost 9 years
    This is so heavyweight. Is there no way to get the old "refresh path" option back in PyCharm 4.5?
  • ecoe
    ecoe over 8 years
    Similar 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
    Josh over 8 years
    This 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
    florisla over 8 years
    The question was not that PyCharm doesn't recognize project code. It doesn't recognize the standard Python built-in methods.
  • Alex
    Alex about 7 years
    I'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
    Mark almost 7 years
    I worked my way down this list and removing/re-adding the interpreter worked for me. Thanks!
  • Bob Stein
    Bob Stein over 6 years
    Similarly 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
    Eduardo Pignatelli almost 6 years
    It is really annoying that such a IDE doesn't take care of such a simple operation.
  • Mike - SMT
    Mike - SMT over 5 years
    You 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
    Jose almost 5 years
    I have pycharm pro and it did not do this for me but after I manually marked the folders correctly it worked
  • Aseem
    Aseem almost 5 years
    It works momentarily. After few minutes I get the same warning again
  • Sameen
    Sameen over 4 years
    Solved my issue on PyCharm 2019.3.3 on MacOS
  • predmod
    predmod about 4 years
    Yes, 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
    predmod about 4 years
    The 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
    Stephen almost 4 years
    Warning: 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
    Floating Sunfish over 3 years
    Apparently, this was my issue. Thanks!
  • deed02392
    deed02392 over 3 years
    Invalidating cache and restarting didn't work for me, but this did!
  • Copy and Paste
    Copy and Paste over 3 years
    Two more tips: 1) If applicable make sure you are using pip3 not pip, especially with remote docker and docker-compose Interpreters. 2) If you are using docker-compose and/or docker playing around with PYTHONPATH can cause these types of issues. More info here: intellij-support.jetbrains.com/hc/en-us/community/posts/…
  • The Unknown Dev
    The Unknown Dev about 3 years
    Worked for me in version 2021.1, after trying all the other tips like reloading skeletons, etc.
  • Denis Barmenkov
    Denis Barmenkov about 3 years
    It will also clean local history.
  • kmiklas
    kmiklas almost 3 years
    I think I love you
  • Salvador Nava
    Salvador Nava almost 3 years
    Simply and easy fix. No other solution worked for me, but this! Thank you so much!
  • Kerwin Sneijders
    Kerwin Sneijders over 2 years
    I 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
    Behrouz.M over 2 years
    just marking the odoo dir as `source root' did the trick
  • Den Kasyanov
    Den Kasyanov over 2 years
    This 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
    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
    scphantm about 2 years
    moving the venv folder out of the project folder worked for me too.
  • chaostheory
    chaostheory about 2 years
    This fixes the problem, but it introduces a new one: you can no longer scan for problems