Python 为什么 PyCharm 会在某些 Numpy 导入中给出未解决的引用错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23689183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:19:19  来源:igfitidea点击:

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

pythonpython-2.7numpypycharm

提问by flutefreak7

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

PyCharm 中的以下行由即时检查标记,每次导入都带有未解决的参考错误。(它们用红色下划线表示。)

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.

我使用这些导入的代码运行良好,没有任何错误或警告。我通常依赖 PyCharm 的红色错误作为我的代码已损坏且无法运行的警告,但在这种情况下 PyCharm 是错误的。

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

为什么 PyCharm 的自省可以识别 numpy 的某些功能而其他功能则不能?

Current Versions:

当前版本:

  • Windows 7 64-bit
  • Python 2.7.5
  • PyCharm 3.1.2
  • Numpy 1.8
  • 视窗 7 64 位
  • 蟒蛇 2.7.5
  • PyCharm 3.1.2
  • 麻木 1.8

Thanks!

谢谢!

采纳答案by Games Brainiac

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

你得到这个的原因是因为 PyCharm 的静态分析。现在,Python 所做的是使用静态骨架(有些是预先生成的,有些是生成的)来进行分析。在这里查看预先生成的骨架 -> 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.

这将阻止错误,它将显示为该行上方的注释。

回答by Giacomo Marciani

The following often helps to solve false-positive unresolved references

以下通常有助于解决误报未解析的引用

File | Invalidate Caches

回答by David Ching

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).

Python 配置在(至少)两个地方指定: Run | Edit Configurations | Python | Python Interpreter, 和File | Settings | Project | Project Interpreter。我的错误是我没有在File | Settings .... 因此,它指的是没有安装导入的 Python 配置(例如 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.

在我将这两个位置设置为指向相同的、正确的 Python 安装后,我做了一个File | Invalidate Caches / Restart,然后就可以了。

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

要检查的第三个地方是File | Default Settings... | Project Interpreter并确保它与其他设置匹配。

回答by bingecoder

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.

您可以禁用对特定库(例如 numpy)的检查。我发现这非常有帮助,因为由于这个问题,我的滚动条一直亮着。转到设置 -> 编辑器 -> 检查 -> Python -> 未解析的引用(靠近底部),然后转到窗口右下角的忽略引用部分。

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

添加一个不带引号的“numpy.*”条目,您将不会再在 numpy 中看到这些未解析的引用点亮您的滚动条!

回答by Simon

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.

我能够简单地使用 virtualenv 而不是系统解释器来解决这个问题。我之前在任何地方找到的其他方法都没有对我有用。

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

我使用的是 Windows 7、PyCharm 社区版 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.pyinside 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->Clickon tandoesn'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 venvfolder.
  5. Now File -> Invalide Caches / Restart ... -> Invalidate and restart
  6. Wait for all the indexing to be done and check whether you can Ctrl->Clickon tanin your script1.py
  1. 创建一个名为 my_project 的新项目并将其设置为使用系统解释器 File -> Settings -> Project: my_project -> Project Interpreter -> Select your project -> Select the system interpreter
  2. script1.py在项目中创建以下测试脚本:

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

    现在运行此脚本工作正常,并打印一些数字,但Pycharm抛出一个悬而未决的参考警告,Ctrl->Clicktan它应该不会去的numpy的代码。

  3. 手动创建虚拟环境

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

    在 Linux 上,将 activate 行替换为 source venv/bin/activate

  4. 告诉 PyCharm 使用虚拟环境:File -> Settings -> Project: my_project -> Project Interpreter -> Select your project -> Select Python 3.6 (my_project)它应该在项目文件夹中的某个位置包含 python.exe venv
  5. 现在 File -> Invalide Caches / Restart ... -> Invalidate and restart
  6. 所有待兴的索引和检查您是否可Ctrl->Clicktan您的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.

通过这种方式,我能够为其他包(如 torch 和 opencv)解决相同的问题(只需创建一个包含我需要的所有包的虚拟环境)。到目前为止没有更多未解决的参考。

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

不知道为什么它会以这种方式工作,但不能与系统解释器一起工作。

回答by psarka

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

PyCharm 开发人员针对检查失败的一个可能原因发布了一种解决方法:

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

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

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

要点 - 如果venv项目目录中有文件夹,检查可能会失败。右键单击它,将目录标记为已排除。

回答by pkm

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

在 PyCharm 的 Project 工具窗口中,右键单击目录并选择 Mark Directory As -> Sources Root。