Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PyCharm give unresolved reference errors on some Numpy imports?

The following line in PyCharm is flagged by on-the-fly inspection with unresolved reference errors for each import. (They are underlined red.)

from numpy import tan, arcsin, arccos, arctan

However the following imports do not cause any error/warning:

from numpy import sin, cos, arctan2, sqrt, cross, pi

The code in which I use these imports runs fine without any errors or warnings. I generally rely on PyCharm's red errors as a warning that my code is broken and will not run, but in this case PyCharm is wrong.

Why are some of numpy's functions recognized by PyCharm's introspection and others aren't?

Current Versions:

  • Windows 7 64-bit
  • Python 2.7.5
  • PyCharm 3.1.2
  • Numpy 1.8

Thanks!

like image 770
flutefreak7 Avatar asked May 15 '14 21:05

flutefreak7


People also ask

How do I fix unresolved reference error in Python?

Add "src" as your source root: Now, add Source to your Python Path: Now, Goto PyCharm Menu: Select "File" --> Invalidate Caches / Restart. Import problem is now resolved.


4 Answers

The reason you are getting this is because of PyCharm's static analysis. Now, what Python does is use static skeletons (some are pre-generated and some are generated) to give you the analysis. Take a look at the pre-generated skeletons here -> https://github.com/JetBrains/python-skeletons

This might be solved, by enabling the following:

enter image description here

However, if that does not work:

enter image description here

which will block off the error, it will appear as a comment above the line.

like image 148
Games Brainiac Avatar answered Sep 23 '22 04:09

Games Brainiac


The Python configuration is specified in (at least) two places: Run | Edit Configurations | Python | Python Interpreter, and File | Settings | Project | Project Interpreter. My mistake was I did not set the correct Python installation in the File | Settings .... Hence, it was referring to a Python configuration that did not have the import installed (e.g. NumPy).

After I set these two locations to point to the same, correct Python installation, I did a File | Invalidate Caches / Restart, then it was fine.

A third place to check is File | Default Settings... | Project Interpreter and make sure it matches the other settings.

like image 44
David Ching Avatar answered Sep 21 '22 04:09

David Ching


PyCharm developer posted a workaround for one possible cause of inspection failure:

https://youtrack.jetbrains.com/issue/PY-32029

Gist of it - inspection may fail if you have a venv folder in the project directory. Right click it, mark directory as excluded.

like image 27
psarka Avatar answered Sep 23 '22 04:09

psarka


The following often helps to solve false-positive unresolved references

File | Invalidate Caches
like image 7
Giacomo Marciani Avatar answered Sep 25 '22 04:09

Giacomo Marciani