zl程序教程

您现在的位置是:首页 >  后端

当前栏目

在pycharm设置conda环境-import opencv-命令行import成功但pycharm import失败

OpencvPycharm命令行conda 环境 设置 成功 失败
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库也正常了