339

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.

davidism
  • 121,510
  • 29
  • 395
  • 339
James McCalden
  • 3,696
  • 2
  • 16
  • 18
  • 1
    I have the same problem and sovled it. [Here solution](http://stackoverflow.com/a/37593582/3001953). – Maks Jun 02 '16 at 13:42

31 Answers31

508

File | Invalidate Caches... and restarting PyCharm helps.

Dmitry Trofimov
  • 7,371
  • 1
  • 30
  • 34
  • 3
    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. – danius Mar 01 '15 at 15:14
  • 10
    This is so heavyweight. Is there no way to get the old "refresh path" option back in PyCharm 4.5? – Chris Withers Aug 26 '15 at 15:13
  • 9
    It is really annoying that such a IDE doesn't take care of such a simple operation. – Eduardo Pignatelli Sep 03 '18 at 11:11
  • 11
    It works momentarily. After few minutes I get the same warning again – Aseem Sep 08 '19 at 02:20
  • 5
    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](https://jetbrains.com/help/pycharm/content-root.html) **"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. – predmod Jun 22 '20 at 20:29
  • 2
    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. – Stephen Aug 11 '20 at 16:58
  • It will also clean local history. – Denis Barmenkov May 05 '21 at 17:21
  • 1
    @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. – deps_stats Apr 03 '22 at 00:13
152

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

Ilian Iliev
  • 3,217
  • 4
  • 26
  • 51
kelorek
  • 6,042
  • 6
  • 29
  • 32
  • 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. – Mike Johnston Jan 15 '14 at 15:15
  • Your tip solved my problem with unresolved built-ins using PyCharm 3.4 EAP. – illagrenan Apr 28 '14 at 18:14
  • 6
    Where is "Python Interpreters". Do you mean "Project Interpreters"? I am using Linux and I couldn't find Paths tab either. – Baskaya Jul 16 '14 at 13:23
  • 1
    In PyCharm 3.4 on OSX, it's an icon which looks like a little folder tree, and doesn't say "Paths". – Daniel Bushman Aug 06 '14 at 22:50
  • Can anybody post a screenshot with that setting? – feeeper Oct 22 '14 at 08:51
  • 14
    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)` – raphaeltm Oct 24 '14 at 15:57
  • 8
    There is no such refresh button in PyCharm 4, only the plus and minus signs. – dtheodor Jan 07 '15 at 10:45
  • 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. – Sarah Messer Feb 03 '15 at 17:43
  • 4
    @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. – Henrik Heimbuerger Feb 11 '15 at 11:13
  • 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. – ecoe Nov 28 '15 at 17:31
  • 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. – Mike - SMT Mar 29 '19 at 18:35
87

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.

florisla
  • 12,668
  • 6
  • 40
  • 47
  • 2
    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. – Alex Jun 20 '17 at 22:36
  • 4
    I worked my way down this list and removing/re-adding the interpreter worked for me. Thanks! – Mark Jul 28 '17 at 15:24
  • 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: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000058690-Module-not-found-in-PyCharm-but-externally-in-Python – Copy and Paste Feb 02 '21 at 02:04
  • 2
    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. – Kerwin Sneijders Oct 04 '21 at 10:44
  • 1
    moving the venv folder out of the project folder worked for me too. – scphantm Apr 08 '22 at 20:28
  • it was the .idea folder smh – Tom Oct 25 '22 at 06:12
34

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"

Shoham
  • 7,014
  • 8
  • 40
  • 40
24

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

dKen
  • 3,078
  • 1
  • 28
  • 37
mujad
  • 643
  • 7
  • 15
  • 8
    I have pycharm pro and it did not do this for me but after I manually marked the folders correctly it worked – Jose Jul 11 '19 at 18:47
  • 5
    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 https://www.jetbrains.com/help/pycharm/content-root.html "PyCharm uses the source roots as the starting point for resolving imports" ! – predmod Jun 22 '20 at 20:23
  • 1
    Simply and easy fix. No other solution worked for me, but this! Thank you so much! – Salvador Nava Aug 25 '21 at 05:47
  • 1
    just marking the odoo dir as `source root' did the trick – Behrouz.M Jan 01 '22 at 04:35
  • This fixes the problem, but it introduces a new one: you can no longer scan for problems – chaostheory Jun 16 '22 at 22:17
  • 1
    This does NOT fix the issue, it hides it. If you mark anything as `excluded` it will be excluded in much more than import checks. It will be excluded from searches and indexing and problem scanning like @chaostheory said too. – Kerwin Sneijders Sep 22 '22 at 20:24
13

I find myself removing and re-adding the remote interpreter to fix this problem when Invalidating Caches or Refreshing Paths does not work.

I use vagrant and every once and awhile if I add a new VM to my multi-vm setup, the forwarded port changes and this seems to confuse PyCharm when it tries to use the wrong port for SSH. Changing the port doesn't seem to help the broken references.

Thomas Farvour
  • 1,103
  • 2
  • 11
  • 24
12

If none of the other solutions work for you, try (backing up) and deleting your ~/.PyCharm40 folder, then reopening PyCharm. This will kill all your preferences as well.

On Mac you want to delete ~/Library/Caches/Pycharm40 and ~/Library/Preferences/PyCharm40.

And on Windows: C:\Users\$USER.PyCharm40.

Imran
  • 12,950
  • 8
  • 64
  • 79
  • 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. – Den Kasyanov Mar 10 '22 at 22:14
11

Tested with PyCharm 4.0.6 (OSX 10.10.3) following this steps:

  1. Click PyCharm menu.
  2. Select Project Interpreter.
  3. Select Gear icon.
  4. Select More button.
  5. Select Project Interpreter you are in.
  6. Select Directory Tree button.
  7. Select Reload list of paths.

Problem solved!

siripan
  • 131
  • 1
  • 8
7

Sorry to bump this question, however I have an important update to make.

You may also want to revert your project interpreter to to Python 2.7.6 if you're using any other version than that This worked for me on my Ubuntu installation of PyCharm 4.04 professional after none of the other recommendations solved my problem.

Fairlight Evony
  • 233
  • 1
  • 3
  • 6
  • 1
    Switching to the local Python 2 interpreter and then back again to your remote Python 3 interpreter fixed this for me too. – James Fletcher Feb 20 '15 at 11:35
  • 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. – Josh Dec 09 '15 at 18:46
  • 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. – Bob Stein Nov 19 '17 at 19:37
7

Much simpler action:

  • File > Settings > Project > Project Interpreter
  • Select "No interpreter" in the "Project interpreter" list
  • Apply > Set your python interpreter again > Click Apply

Profit - Pycharm is updating skeletons and everything is fine.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Alexander.Iljushkin
  • 4,519
  • 7
  • 29
  • 46
5

If you want to ignore only some "unresolved reference" errors, you can also tell it PyCharm explicitly by placing this in front of your class/method/function:

# noinspection PyUnresolvedReferences
flix
  • 1,821
  • 18
  • 23
4

You might try closing Pycharm, deleting the .idea folder from your project, then starting Pycharm again and recreating the project. This worked for me whereas invalidating cache did not.

Jason McVetta
  • 1,383
  • 11
  • 11
3

I finally got this working after none of the proposed solutions worked for me. I was playing with a django rest framework project and was using a virtualenv I had setup with it. I was able to get Pycharm fixed by marking the root folder as the sources root, but then django's server would throw resolve exceptions. So one would work when the other wouldn't and vice versa.

Ultimately I just had to mark the subfolder as the sources root in pycharm. So my structure was like this

-playground
     -env
     -playground

That second playground folder is the one I had to mark as the sources root for everything to work as expected. That didn't present any issues for my scenario so it was a workable solution.

Just thought I'd share in case someone else can use it.

user2233949
  • 2,053
  • 19
  • 22
3

It could also be a python version issue. I had to pick the right one to make it work. enter image description here

Shehaaz
  • 753
  • 1
  • 7
  • 12
3

None of the answers solved my problem.

What did it for me was switching environments and going back to the same environment. File->Settings->Project interpreter

I am using conda environments.

kkica
  • 4,034
  • 1
  • 20
  • 40
3

Mine got resolved by checking inherit global site-packages in PyCharm

File -> Settings -> Project Interpreter -> Add Local Interpreter -> Inherit global site-packages

Abhishek Kumar
  • 383
  • 4
  • 18
1

I closed all the other projects and run my required project in isolation in Pycharm. I created a separate virtualenv from pycharm and added all the required modules in it by using pip. I added this virtual environment in project's interpreter. This solved my problem.

Waqar Detho
  • 1,502
  • 18
  • 17
1

Geeze what a nightmare, my amalgamation of different StackOVerflow answers:

  1. Switch to local interpreter /usr/bin/pythonX.X and apply
  2. View paths like above answer
  3. Find skeletons path. Mine was (/home/tim/Desktop/pycharm-community-2016.2.3/helpers/python-skeletons)
  4. Switch back to virt interpreter and add the skeletons path manually if it didn't automatically show up.
Tim
  • 284
  • 1
  • 4
  • 20
1

None of the above solutions worked for me!
If you are using virtual environment for your project make sure to apply the python.exe file that is inside your virtual environment directory as interpreter for the project (Alt + Ctrl + Shift + S) this solved the issue for me.

Ali Sherafat
  • 3,506
  • 2
  • 38
  • 51
1

In my case the inspection error shows up due to a very specific case of python code. A min function that contains two numpy functions and two list accesses makes my code inspection give this kind of errors.

Removing the 'd=0' line in the following example gives an unresolved reference error as expected, but readding doesn't make the error go away for the code inspector. I can still execute the code without problems afterwards.

import numpy as np
def strange(S, T, U, V):
    d = 0
    print min(np.abs(S[d]), np.abs(T[d]), U[d], V[d])

Clearing caches and reloading list of paths doesn't work. Only altering the code with one of the following example patches does work:

  • Another ordering of the 'min' parameters: schematically S U T V but not S T U V or T S U V
  • Using a method instead of the function: S[d].abs() instead of np.abs(S[d])
  • Using the built-in abs() function
  • Adding a number to a parameter of choice: U[d] + 0.
1

My problem is that Flask-WTF is not resolved by PyCharm. I have tried to re-install and then install or Invalidate Cache and Restart PyCharm, but it's still not working.

Then I came up with this solution and it works perfectly for me.

  1. Open Project Interpreter by Ctrl+Alt+S (Windows) and then click Install (+) a new packgage.

enter image description here

  1. Type the package which is not resolved by PyCharm and then click Install Package. Then click OK.

enter image description here

Now, you'll see your library has been resolved.

Hien Quang Le
  • 158
  • 2
  • 6
1

In PyCharm 2020.1.4 (Community Edition) on Windows 10 10.0. Under Settings in PyCharm: File > Settings > Project Structure I made two changes in Project Structure: main folder marked as source and odoo folder with all applications I excluded Screenshot shows what I did. After that I restarted PyCharm: File > Invalidate Caches / Restart...

Unresolved references error was removed project structure

Unresolved references error

1

Invalidating the cache as suggested by other answers did not work for me. What I found to be the problem in my case was that PyCharm was marking init.py files of Python packages as text and thus not including them in the analysis which means python resolving was not working correctly.

The solution for me was to:

Open PyCharm settings Navigate to Editor -> File Types Find Python and add __init__.py to the list of python files or Find Text and delete __init__.py from the list of text files

enter image description here

Ouss
  • 2,912
  • 2
  • 25
  • 45
1

To add yet another one: None of the solutions involving the Python Interpreter tab helped, however, I noticed I had to set Project Dependencies: In the project that had unresolved reference errors, none of the dependencies were checked. Once I checked them, the relevant errors disappeared. I don't know why some are checked to begin with and others are not.

enter image description here

christianbrodbeck
  • 2,113
  • 2
  • 19
  • 24
0

If you are using vagrant the error can be caused by wrong python interpreter. In our vagrant we are using pyenv so I had to change Python Interpreter path path from /usr/bin/python to /home/vagrant/.pyenv/versions/vagrant/bin/python enter image description here

Danil
  • 4,781
  • 1
  • 35
  • 50
0

I have a project where one file in src/ imports another file in the same directory. To get PyCharm to recognize I had to to go to File > Settings > Project > Project Structure > select src folder and click "Mark as: Sources"

From https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html

Source roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports

qwr
  • 9,525
  • 5
  • 58
  • 102
0

I had to go to File->Invalidate Caches/Restart, reboot Ubuntu 18.04 LTS, then open Pycharm and File-> Invalidate Caches/Restart again before it cleared up.

seizouki
  • 55
  • 6
0

For me it helped: update your main directory "mark Directory as" -> "source root"

Valli
  • 11
  • 3
0

@kelorek works for me, but before, in interpereter paths I had to add some path. lets say

from geometry_msgs.msg import Twist

is underline as error, then in remote machine in python run:

help("geometry_msgs")

at the end there will be path lets say :

/opt/ros/foxy/lib/python3.8/site-packages/geometry_msgs/__init__.py

so to Your intepreter pycharm path add

/opt/ros/foxy/lib/python3.8/site-packages

Hope it will help You and it helps me :)

Adrian
  • 84
  • 2
  • 6
0

I had the same symptoms. In my instance the problem source was that I had set idea.max.intellisense.filesize=50 in the custom properties. I could resolve it by setting it to 100.

Help->Edit Custom Properties

Nico
  • 73
  • 1
  • 9
-1

Are you using virtualenv?

if so, you need to notify PyCharm for every change in the location of the the desired python.exe (merely ./activate is not enough for PyCharm)

Make sure Pycharm points to the correct interpetor and packages: File -> Settings -> Project -> Project Interpreter. Click the gear and choose python.exe under virtualenv's Scripts folder

enter image description here

Bar Horing
  • 5,337
  • 3
  • 30
  • 26