PyCharm shows unresolved references error for valid code

PythonPycharm

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

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

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

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

Solution 6 - Python

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.

Solution 7 - Python

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.

Solution 8 - Python

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!

Solution 9 - Python

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.

Solution 10 - Python

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

Solution 11 - Python

Worked for me with much simplier 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.

Solution 12 - Python

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.

Solution 13 - Python

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.

Solution 14 - Python

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

Solution 15 - Python

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.

Solution 16 - Python

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.

Solution 17 - Python

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.

Solution 18 - Python

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.

Solution 19 - Python

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.

Solution 20 - Python

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.

Solution 21 - Python

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

Solution 22 - Python

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

Solution 23 - Python

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

Solution 24 - Python

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

Solution 25 - Python

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.

Solution 26 - Python

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

Solution 27 - Python

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

Solution 28 - Python

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

Solution 29 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames McCaldenView Question on Stackoverflow
Solution 1 - PythonDmitry TrofimovView Answer on Stackoverflow
Solution 2 - PythonkelorekView Answer on Stackoverflow
Solution 3 - PythonflorislaView Answer on Stackoverflow
Solution 4 - PythonShohamView Answer on Stackoverflow
Solution 5 - PythonmujadView Answer on Stackoverflow
Solution 6 - PythonThomas FarvourView Answer on Stackoverflow
Solution 7 - PythonImranView Answer on Stackoverflow
Solution 8 - PythonsiripanView Answer on Stackoverflow
Solution 9 - PythonFairlight EvonyView Answer on Stackoverflow
Solution 10 - PythonflixView Answer on Stackoverflow
Solution 11 - PythonAlexander.IljushkinView Answer on Stackoverflow
Solution 12 - PythonJason McVettaView Answer on Stackoverflow
Solution 13 - Pythonuser2233949View Answer on Stackoverflow
Solution 14 - PythonShehaazView Answer on Stackoverflow
Solution 15 - PythonkkicaView Answer on Stackoverflow
Solution 16 - PythonWaqar DethoView Answer on Stackoverflow
Solution 17 - PythonTimView Answer on Stackoverflow
Solution 18 - PythonAli SherafatView Answer on Stackoverflow
Solution 19 - PythonWannes GoethalsView Answer on Stackoverflow
Solution 20 - PythonHien Quang LeView Answer on Stackoverflow
Solution 21 - PythonIvica ČevisView Answer on Stackoverflow
Solution 22 - PythonOussView Answer on Stackoverflow
Solution 23 - PythonDaniil MashkinView Answer on Stackoverflow
Solution 24 - PythonqwrView Answer on Stackoverflow
Solution 25 - PythonseizoukiView Answer on Stackoverflow
Solution 26 - PythonValliView Answer on Stackoverflow
Solution 27 - PythonAdrianView Answer on Stackoverflow
Solution 28 - PythonNico RöselView Answer on Stackoverflow
Solution 29 - PythonBar Horing AmirView Answer on Stackoverflow