在pycharm设置conda环境-import opencv-命令行import成功但pycharm import失败
2023-09-14 09:13:57 时间
问题
使用opencv
brew install opencv // 安装opencv库
pip install opencv-python // 安装opencv的python版SDK
命令行测试opencv确实可用(因为import成功了)
(base) ✘ ~ python
Python 3.8.8 (default, Apr 13 2021, 12:59:45)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
但pycharm却未引入cv2的库, 如下图
分析
众所周知, python有很多版本, 如下
(base) ~ ll /usr/local/bin/python*
lrwxr-xr-x 1 sunyuchuan admin 42B 8 16 16:38 /usr/local/bin/python3 -> ../Cellar/python@3.10/3.10.6_1/bin/python3
lrwxr-xr-x 1 sunyuchuan admin 49B 8 16 16:38 /usr/local/bin/python3-config -> ../Cellar/python@3.10/3.10.6_1/bin/python3-config
lrwxr-xr-x 1 sunyuchuan admin 45B 8 16 16:38 /usr/local/bin/python3.10 -> ../Cellar/python@3.10/3.10.6_1/bin/python3.10
lrwxr-xr-x 1 sunyuchuan admin 52B 8 16 16:38 /usr/local/bin/python3.10-config -> ../Cellar/python@3.10/3.10.6_1/bin/python3.10-config
lrwxr-xr-x 1 sunyuchuan admin 43B 8 16 16:42 /usr/local/bin/python3.9 -> ../Cellar/python@3.9/3.9.13_3/bin/python3.9
lrwxr-xr-x 1 sunyuchuan admin 50B 8 16 16:42 /usr/local/bin/python3.9-config -> ../Cellar/python@3.9/3.9.13_3/bin/python3.9-config
(base) ✘ ~ which python2
/usr/bin/python2
(base) ~ which python3
/Users/admin/opt/anaconda3/bin/python3
其实这些众多python版本, 都被各种其他库使用, 还没办法删
(base) ~ brew uninstall python@3.10
Error: Refusing to uninstall /usr/local/Cellar/python@3.10/3.10.6_1
because it is required by autojump, gobject-introspection, gts, opencv, pgcli, python-tabulate and vtk, which are currently installed.
You can override this and force removal with:
brew uninstall --ignore-dependencies python@3.10
(base) ✘ ~ brew uninstall python@3.9
Error: Refusing to uninstall /usr/local/Cellar/python@3.9/3.9.13_3
because it is required by cairo, graphviz, libass and librsvg, which are currently installed.
You can override this and force removal with:
brew uninstall --ignore-dependencies python@3.9
anaconda
anaconda是各版本python的管理器, 可以设置很多套环境, 每个环境有不同的python版本, 和不同的库
例如base环境, 安装了python3.8.8, 安装了python-opencv库, 安装了matplotlib库
例如a环境, 安装了python3.10, 安装了flask的web框架
我们可以随意切换, 其操作命令如下
(base) ~ conda
usage: conda [-h] [-V] command ...
conda is a tool for managing and deploying applications, environments and packages.
Options:
positional arguments:
command
clean Remove unused packages and caches.
compare Compare packages between conda environments.
config Modify configuration values in .condarc. This is modeled after the git config command. Writes to the user .condarc file (/Users/sunyuchuan/.condarc) by default.
create Create a new conda environment from a list of specified packages.
help Displays a list of available conda commands and their help strings.
info Display information about current conda install.
init Initialize conda for shell interaction. [Experimental]
install Installs a list of packages into a specified conda environment.
list List linked packages in a conda environment.
package Low-level conda package utility. (EXPERIMENTAL)
remove Remove a list of packages from a specified conda environment.
uninstall Alias for conda remove.
run Run an executable in a conda environment. [Experimental]
search Search for packages and display associated information. The input is a MatchSpec, a query language for conda packages. See examples below.
update Updates conda packages to the latest compatible version.
upgrade Alias for conda update.
optional arguments:
-h, --help Show this help message and exit.
-V, --version Show the conda version number and exit.
conda commands available from other packages:
build
content-trust
convert
debug
develop
env
index
inspect
metapackage
render
repo
server
skeleton
token
verify
所以pycharm也需要设置解释器, 在Setting->Python Interpreter->
我们已在pycharm添加了base环境conda的python解释器, 此时pycharm里的python-opencv库也正常了
相关文章
- Android版OpenCV图像处理技术亲自验证[四十二]之图像中检测霍夫线(附源码)
- OpenCV每日函数 图像过滤模块 (10) getGaborKernel计算Gabor 滤波器函数
- OpenCV每日函数 结构分析和形状描述符(4) findContours函数寻找轮廓
- Opencv学习笔记 基于Haar级联的人脸检测/识别
- Opencv学习笔记 使用opencv读取mp4并生成Gif动图
- 成功解决sub_div.insert(points)cv2.error: OpenCV(3.4.1) C:projectsopencv-pythonopencvmodulesimgproc
- 成功解决cv2.error: OpenCV(4.1.2) C:projectsopencv-pythonopencvmodulesimgprocsrccolor.cpp:182: err
- yolov4视频目标检测:使用C++版本联合CUDA11.2的OpenCV 5.x编译生成opencv-python==5.x进行推理
- 在OpenCV里学习常见问题汇编24
- 在OpenCV里使用SVM识别手写数字
- 在OpenCV里实现二维离散卷积3
- OpenCV入门(十二)快速学会OpenCV 11几何变换
- OpenCV入门(二)快速学会OpenCV1图像基本操作
- OpenCV如何在图像上绘制几何形状和文本?
- 一招解决报错error: OpenCV(4.2.0) C:projectsopencv-pythonopencvmodulesobjdetectsrccascadedetect.cpp
- macOS python3 opencv 读取和写入图像