'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!



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

The following often helps to solve false-positive unresolved references

File | Invalidate Caches

Solution 4:[4]

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.

Solution 5:[5]

In PyCharm's Project tool window, right-click on the directory and select Mark Directory As -> Sources Root.

Solution 6:[6]

You can disable inspections to specific libraries (such as numpy). I found this very helpful since my scrollbar was constantly lit up all over due to this issue. Go to Settings -> Editor -> Inspections -> Python -> Unresolved references (near the bottom) and go to the Ignore references section at the bottom right of the window.

Add an entry with "numpy.*" without the quotes and you won't see these unresolved references in numpy lighting up your scrollbar any more!

Solution 7:[7]

I was able to resolve the issue simply using a virtualenv instead of the system interpreter. None of the other methods i found anywhere worked for me before.

I am using Windows 7, PyCharm Community Edition 2018.2.4, Python 3.6.7, Numpy 1.15.4

  1. Create a new project named my_project and set it to use the system interpreter File -> Settings -> Project: my_project -> Project Interpreter -> Select your project -> Select the system interpreter
  2. Create following test script script1.py inside the project:

    import numpy as np
    print(np.tan(8))
    

    Now running this script works fine and prints some number, but Pycharm throws an unresolved reference warning and Ctrl->Click on tan doesn't go to the numpy code as it should.

  3. Manually create the virtual environment

    $cd dir/to/my_project
    $virtualenv venv
    $venv\Scripts\activate
    (venv) $pip install numpy
    (venv) $deactivate
    

    On Linux, replace the activate line with source venv/bin/activate

  4. Tell PyCharm to use the virtual environment: File -> Settings -> Project: my_project -> Project Interpreter -> Select your project -> Select Python 3.6 (my_project) which should have the python.exe inside your project folder somewhere in the venv folder.
  5. Now File -> Invalide Caches / Restart ... -> Invalidate and restart
  6. Wait for all the indexing to be done and check whether you can Ctrl->Click on tan in your script1.py

This way I was able to fix the same problem for other packages like torch and opencv (simply creating a virtual environment with all the packages I need). No more unresolved references so far.

No idea why it would work this way but would not work with the system interpreter.

Solution 8:[8]

What worked for me was Settings > project > python interpreters > (cog wheel to the right of text bar) Show all > (on python venv) enable associate this virtual environment with (project path)

Solution 9:[9]

You may have to install the package using pip install numpy

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Games Brainiac
Solution 2 rkedge
Solution 3 Giacomo Marciani
Solution 4 psarka
Solution 5 prashant.kr.mod
Solution 6 bingecoder
Solution 7 Gulzar
Solution 8 Hackerman
Solution 9 Onawola Gboyega