Why does PyCharm give unresolved reference errors

2019-01-17 15:43发布

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!

6条回答
Lonely孤独者°
2楼-- · 2019-01-17 15:55

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.

查看更多
Melony?
3楼-- · 2019-01-17 16:01

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.

查看更多
祖国的老花朵
4楼-- · 2019-01-17 16:03

The following often helps to solve false-positive unresolved references

File | Invalidate Caches
查看更多
手持菜刀,她持情操
5楼-- · 2019-01-17 16:10

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.

查看更多
Emotional °昔
6楼-- · 2019-01-17 16:13

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!

查看更多
贪生不怕死
7楼-- · 2019-01-17 16:16

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
    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
    pip install numpy
    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.

查看更多
登录 后发表回答