【Pycharm】设置conda环境:import opencv:命令行 import 成功,但 pycharm import 失败

文章目录

问题

使用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库也正常了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值