PyCharm 中针对 Python 编译扩展的自动完成

发布于 2024-10-18 10:16:53 字数 253 浏览 6 评论 0原文

当使用编译扩展(例如 OpenCV Python 绑定)编写 Python 代码时,PyCharm 似乎不知道它们的可用性。导入标有灰色下划线,作为工具提示显示“未解析的引用”,并且自动完成功能也不起作用。 (代码中已使用的函数名称除外。)

这不是由错误的模块路径引起的,代码在启动时运行没有错误。另外,在 Python shell 中导入模块后,自动完成功能开始按预期工作。

是否有解决方案,或者这是编译扩展的架构限制?还有其他 IDE 可以解决这个问题吗?

When writing Python code using compiled extensions (the OpenCV Python bindings, for example), PyCharm doesn't seem to be aware of their availability. The imports are marked with a grey underline, saying "unresolved reference" as a tooltip, and autocomplete doesn't work, either. (Except for the function names already used in the code.)

This isn't caused by wrong module paths, the code runs without error when started. Also, after I import the modules in a Python shell, autocomplete starts working as expected.

Is there a solution for that or is this an architectural limitation for compiled extensions? Are there any other IDEs that manage to cope with this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

夏雨凉 2024-10-25 10:16:53

导入标有灰色下划线,表示“未解析的引用”作为工具提示

这很可能意味着 PyCharm 无法看到您导入的模块。在编辑模式下,PyCharm 依赖于导入模块的 Python 源的可用性。如果模块不是用 Python 编写的,而是 C 扩展模块,PyCharm 会生成一个包含函数原型的“骨架”,并使用它来完成。

在 shell 模式下,PyCharm 使用实时导入的对象来完成,结果略有不同。

确保您为项目选择的 Python 解释器可以看到 OpenCV 安装(文件/设置/Python 解释器)。如果解释器是正确的,请尝试删除并重新添加它(这有点耗时,抱歉)。

如果没有帮助,请提交错误

The imports are marked with a grey underline, saying "unresolved reference" as a tooltip

This most probably means that PyCharm can't see the module you import. In editing mode, PyCharm relies on availability of Python sources of imported modules. If a module is not written in Python but is a C extension module, PyCharm generates a 'skeleton' that contains function prototypes, and uses it for completion.

In shell mode, PyCharm uses live imported objects for completion, with slightly different results.

Make sure that your OpenCV installation is visible for the Python interpreter you chose for the project (File / Settings / Python interpreter). If the interpreter is correct, try removing and re-adding it (this is time-consuming a bit, sorry).

If nothing helps, file a bug.

仙女山的月亮 2024-10-25 10:16:53

我注意到 pycharm 行为的差异取决于导入方式。
using:

import cv2

自动完成不起作用,

而 with:

from cv2 import cv2

自动完成起作用

I have noticed a difference in pycharm behavior depending on the way to import.
using:

import cv2

the auto completion doesn't work,

while with:

from cv2 import cv2

auto completion works

迷离° 2024-10-25 10:16:53

我必须将二进制文件硬链接到解释器的 lib-dynload 文件夹中。

$ cd /usr/lib/python3.7/lib-dynload
$ sudo ln /usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so cv2.cpython-37m-x86_64-linux-gnu.so

这些路径可能因您的环境而异。我没有在 OSX 或 Windows 上测试它,但它也可能在那里工作。 lib-dynload 文件夹位于:
项目结构

I had to hardlink the binary into the folder lib-dynload of my interpreter.

$ cd /usr/lib/python3.7/lib-dynload
$ sudo ln /usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so cv2.cpython-37m-x86_64-linux-gnu.so

The paths may vary in your environment. I didn't test it on OSX or Windows, but it may work there too. The lib-dynload folder is here:
Project Structure

晨曦÷微暖 2024-10-25 10:16:53

PyCharm 目前不扫描手动添加到 IDE 解释器的路径中的已编译扩展/二进制文件。我已在 YouTrack 中向 Jetbrains 提交了一个错误。您可能想看看它以及我在他们的讨论论坛中发起的讨论(链接位于错误描述中)。如果您是面临同样问题的 PyCharm 用户,如果您可以投票支持在 YouTrack 中解决此问题,我将不胜感激。

PyCharm currently does not scan compiled extensions/binaries which are in a path manually added to the interpreter in the IDE. I have filed a bug with Jetbrains in YouTrack. You might want to have a look at it and possibly the discussion I initiated in their discussion forum (link is in the bug description). I'd appreciate if you could vote for this issue to be resolved in YouTrack if you are a PyCharm user facing the same problem.

像你 2024-10-25 10:16:53

尝试单击“文件|”中的“重新加载”按钮设置 | IDE 设置 | Python 解释器。这让它对我有用。

Try clicking "Reload" button in File | Settings | IDE Settings | Python interpreters. That got it working for me.

混浊又暗下来 2024-10-25 10:16:53

在我的 OS X 10.8 和 PyCharm 3 上,IDE 会自动选择不同的 Python 安装。我在 Eclipse Pydev 中注意到了这一点,它选择了正确的并按预期工作。注意到两者之间的区别并不容易:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python

In my case on OS X 10.8 and PyCharm 3, IDE was automatically picking different installations of Python. I noticed this in Eclipse Pydev, which picked up the one right one and worked as expected. It was not easy to notice the difference between the two:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python

七色彩虹 2024-10-25 10:16:53

我按照这个问题下的说明进行操作:
如何在 Windows 上安装 OpenCV 并在不使用包管理器的情况下为 PyCharm 启用它

之后它不起作用,我重新安装了 pycharm ide,没有进行任何其他更改,现在它工作正常。
我知道这不是最好的答案,但在浪费了大量时间并尝试不同的解决方法之后,这是解决我的问题的答案,我希望它可以帮助您。

I follow the instructions under this question:
How to install OpenCV on Windows and enable it for PyCharm without using the package manager

After that it does not work and I reinstall the pycharm ide without any other changes and now it is working perfectly.
I know that this is not the best answer, but after a lot of time wasted and trying different workarounds this was the one that solve my problem, I hope it can help you.

情独悲 2024-10-25 10:16:53

经过两天的测试,终于解决了这个问题:
区别:

  1. 卸载python 3.7.2,安装python 3.7.7。
  2. 更改python安装路径。(我强烈怀疑原因是我的opencv-python的PATH中有一些汉字。它应该只有英文)。

然后执行以下操作:

  1. 安装opencv-contrib-python

After two days test,I finally fix this issue:
The difference:

  1. Uninstall python 3.7.2,install python 3.7.7.
  2. Change the path where python install.(I strongly doubt that the cause is that my PATH of opencv-python has some Chinese characters.It should have only English).

Then do:

  1. Install the opencv-contrib-python.
你在看孤独的风景 2024-10-25 10:16:53

在最新版本(opencv-python==4.7.0.72)中,文件名更改为cv2.abi3.so。我必须在文件下面添加链接才能自动完成工作。

sudo ln /home//anaconda3/envs//lib/python3.10/site-packages/cv2/cv2.abi3.so cv2.cpython-37m-x86_64-linux -gnu.so

操作系统:Ubuntu 22.04

In the latest version (opencv-python==4.7.0.72), file name is changed to cv2.abi3.so. I had to do link below file for auto complete to work.

sudo ln /home/<usrname>/anaconda3/envs/<env-name>/lib/python3.10/site-packages/cv2/cv2.abi3.so cv2.cpython-37m-x86_64-linux-gnu.so

OS: Ubuntu 22.04

栩栩如生 2024-10-25 10:16:53

我不想给出“对我有用”的答案,但也许我的环境的详细信息将帮助您确定最终的问题。

我以前从未使用过PyCharm,但我刚刚使用PyCharm 1.1.1在Mac 10.6.6上进行了测试,使用Macports opencv +python26。自动完成功能第一次对我来说效果很好。我还关闭并重新运行 PyCharm,并且无需进一步执行任何操作即可自动完成。我尝试过的其他本机扩展(如 cjson、procname)的自动完成也没有问题。

Pycharm 1.1.1 导入带有自动完成功能的 opencv

也许这是一个特定于平台的问题(Windows?),或者是影响旧版本 PyCharm 的错误?

I hate to give a "works for me" answer, but maybe the details on my environment will help you identify the problem on your end.

I've never used PyCharm before, but I just did a test on Mac 10.6.6 using PyCharm 1.1.1, with Macports opencv +python26. The autocomplete worked fine for me the first time. I also closed and re-ran PyCharm and was able to autocomplete without doing anything further. I also had no issue with autocomplete for other native extensions I tried like cjson, procname.

Pycharm 1.1.1 importing opencv with autocomplete.

Perhaps it is a platform-specific issue (Windows?), or a bug affecting an older version of PyCharm?

执手闯天涯 2024-10-25 10:16:53

就我而言,将 opencv 包含在路径中 install-opencv-4-on-windows。并将其添加到项目设置中,如果这些都不适合您,我建议您安装 anaconda 更改 python 解释器并使用 anaconda 解释器。

为此,请转到: file ->设置->项目:测试-> python 解释器 并选择 conda 解释器(

如果您没有 anaconda,可以在 https://www.anaconda 下载)。 com/

按照链接 python 中的步骤操作-opencv 在anaconda中安装opencv

In my case, include opencv in the path install-opencv-4-on-windows. and add it to the project settings, if none of this works for you, I recommend that you install anaconda change the python interpreter and use the anaconda interpreter.

for this go to : file -> settings -> project:test -> python interpreter and select conda interpreter

if you dont have anaconda you can download at https://www.anaconda.com/

follow the steps in the link python-opencv to install opencv in anaconda

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文