Python PyCharm 无法将 cv2 识别为模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34365044/
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
PyCharm does not recognize cv2 as a module
提问by Abd ElRahman Abbas
I am using OpenCV 3 and python 2.7 and coding using PyCharm. The code works fine but PyCharm does not recognize cv2 as a module. It underlines it with a red line, so it doesn't display its functions in the intellisence menu.
我正在使用 OpenCV 3 和 python 2.7 并使用 PyCharm 进行编码。代码工作正常,但 PyCharm 无法将 cv2 识别为模块。它用红线强调它,因此它不会在智能菜单中显示其功能。
I tried to set an environment variable OPENCV_DIR but it didn't work
我试图设置一个环境变量 OPENCV_DIR 但它没有用
OpenCV is extracted in F:\opencv and Python is installed on C:\Python27
OpenCV 解压在 F:\opencv 中,Python 安装在 C:\Python27
What is wrong ?
怎么了 ?
回答by ILA
I have the same problem and I'm afraid that there isn't a solution for this at the moment. You can read more about the issue here
我有同样的问题,恐怕目前没有解决方案。您可以在此处阅读有关该问题的更多信息
The problem is that OpenCVdoesn't include the needed .py
file to have the autocomplete which might be why PyCharm isn't picking up the package as well.
问题是OpenCV不包含.py
自动完成所需的文件,这可能是 PyCharm 也没有提取包的原因。
To be sure that we are on the same page, when you go File>Default Settings>Default Project>Project Interpreter and select the python 2.7 interpreter, does it list cv2
or opencv
in the packages?
为了确保我们在同一页面上,当您转到 File>Default Settings>Default Project>Project Interpreter 并选择 python 2.7 解释器时,它是在列表中cv2
还是opencv
在包中?
EDIT:I was able to get it working with anaconda instead of python. Just instaled anaconda and did everything the same as I would with python (copied cv2 where it would be in python). Hope it helps you!
编辑:我能够让它与 anaconda 而不是 python 一起工作。刚刚安装了 anaconda 并完成了与使用 python 相同的所有操作(将 cv2 复制到 python 中的位置)。希望对你有帮助!
回答by webapp
Follow the link How to install OpenCV on Windows and enable it for PyCharm without using the package manager
按照链接如何在 Windows 上安装 OpenCV 并在不使用包管理器的情况下为 PyCharm 启用它
Steps to follow:
要遵循的步骤:
Install Python 2.7.10
Install Pycharm(If you have not done it already)
Download and install the OpenCV executable.
Add OpenCV in the system path(%OPENCV_DIR% = /path/of/opencv/directory)
Goto C:\opencv\build\python.7\x86 folder and copy cv2.pyd file.
Goto C:\Python27\DLLs directory and paste the cv2.pyd file.
Goto C:\Python27\Lib\site-packages directory and paste the cv2.pyd file.
Goto PyCharm IDE and goto DefaultSettings>PythonInterpreter.
Select the Python which you have installed on Step1.
Install the packages numpy,matplotlib and pip in pycharm.
Restart your PyCharm.
PyCharm now has OpenCV library installed and working.
回答by Denis Romasanta
I follow the steps in the webapp response and after that It does not work and I decided to reinstall the pycharm IDE, this works for me. Hope it helps.
我按照 webapp 响应中的步骤操作,之后它不起作用,我决定重新安装 pycharm IDE,这对我有用。希望能帮助到你。
回答by Guru
Worked on Pycharm 4.0 version
在 Pycharm 4.0 版本上工作
Follow these steps:
按着这些次序:
Go to File>Settings in Pycharm IDE Window
Search Project Interpreter in search bar.
- Click on any package from the available options
- Package window will open from where you can install any packages.
转到Pycharm IDE 窗口中的文件>设置
在搜索栏中搜索项目解释器。
- 从可用选项中单击任何包
- 包窗口将打开,您可以在其中安装任何包。
I have installed opencv-python package to run opencv commands
我已经安装了 opencv-python 包来运行 opencv 命令
回答by geekly
Try File->Invalidate Caches / Restart...
尝试文件->无效缓存/重新启动...
I can't say definitively why this works, but it may have something to do with the cached module definitions that PyCharm uses to provide code hints. In some cases they aren't updated or get corrupted. I just know that it's worked for me.
我不能确切地说为什么会这样,但它可能与 PyCharm 用来提供代码提示的缓存模块定义有关。在某些情况下,它们不会更新或损坏。我只知道它对我有用。
回答by Talha Farooq
You can install your existing libraries to pycharm by enabling the button "Inherit global site-packages" while creating project.
If you don't have installed libraries then you can install it by going to File>Settings>Project:your project name>project interpreter
and then install your required package by searching that.
您可以通过在创建项目时启用“继承全局站点包”按钮来将现有库安装到 pycharm。如果您还没有安装库,那么您可以通过转到安装它File>Settings>Project:your project name>project interpreter
,然后通过搜索安装所需的包。
回答by atreyHazelHispanic
After installing OpenCV with pip and then pip3 in terminal. It would import when writing python in terminal, but not in PyCharm. I tried the invalidation of cache mentioned above, and it worked for a min until cache was warmed up. Same result....
I fixed it by going to:
使用 pip 安装 OpenCV 后,然后在终端中使用 pip3。在终端中编写 python 时会导入,但在 PyCharm 中不会。我尝试了上面提到的缓存失效,它工作了一分钟,直到缓存预热。同样的结果......
我通过去修复它:
- PyCharm Menu
- Preferences
- Project (proj name) -> Project Interpreter (this time instead of CV2)
- Plus sign (to install packages)
- searched for opencv-python
- installed the package
- PyCharm 菜单
- 喜好
- 项目(项目名称)-> 项目解释器 (这次代替CV2)
- 加号(安装软件包)
- 搜索 opencv-python
- 安装了软件包
I didn't even have to dot off that library, it then accepted the "import cv2"
我什至不必点掉那个库,然后它就接受了“导入 cv2”
回答by Navod B ak
Just install opencv python package from settings.
只需从设置中安装 opencv python 包。
回答by Max
Just:
只是:
pip install opencv-python
solved for me on Windows and Linux.
在 Windows 和 Linux 上为我解决了。
回答by Hareesh Kesireddy
Installing opencv-python package from pycharm setting worked for me.
从 pycharm 设置安装 opencv-python 包对我有用。