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!
The Python configuration is specified in (at least) two places:
Run | Edit Configurations | Python | Python Interpreter
, andFile | Settings | Project | Project Interpreter
. My mistake was I did not set the correct Python installation in theFile | 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.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:
However, if that does not work:
which will block off the error, it will appear as a comment above the line.
The following often helps to solve false-positive unresolved references
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.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!
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
File -> Settings -> Project: my_project -> Project Interpreter -> Select your project -> Select the system interpreter
Create following test script
script1.py
inside the project:Now running this script works fine and prints some number, but Pycharm throws an unresolved reference warning and
Ctrl->Click
ontan
doesn't go to the numpy code as it should.Manually create the virtual environment
On Linux, replace the activate line with
source venv/bin/activate
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 thevenv
folder.File -> Invalide Caches / Restart ... -> Invalidate and restart
Ctrl->Click
ontan
in yourscript1.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.