文章框架
背景:
本次是关于在Mac系统下,怎么给anaconda添加OpenCV库。虽然anaconda本身带有丰富的库了,但是如要用到OpenCV(图像处理库),Tensorflow(机器学习库)等,还是需要手动进行配置,本文就是基于Mac系统下给anaconda的配置,并将配置好的python解释器添加到Pycharm IDE 中。
方法步骤:
1.首先,我们可以在终端查看一下安装好的anaconda有哪些库,命令如下:
~/ conda list
2.根据其他教程,正常的安装Opencv3 的命令是:
~/ conda install -c menpo opencv
但是我在安装的时候出现这样的如下错误
to be incompatible with the existing python installation in your environment:
Specifications:
- opencv3 -> python[version='2.7.*|3.4.*|3.5.*']
Your python: python=3.7
If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that
原因是因为在当前的环境下,也就是那个console的环境下,要安装Opencv3,出现了版本不对应的情况。那个-c是什么意思呢?它后面跟的是一个安装源url,这里的menpo指的是一个安装源。
所以我们可以单独重新创造一个环境来安装OpenCV,以下我们创建一个name=ComputerVision的文件来下载python3.7,代码如下:
$ conda create --name ComputerVision python=3.7
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.8.2
latest version: 4.8.3
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: /Applications/anaconda3/envs/ComputerVision
added / updated specs:
- python=3.7
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2020.4.5.1 | py37_0 155 KB
openssl-1.1.1g | h1de35cc_0 2.2 MB
pip-20.0.2 | py37_3 1.7 MB
python-3.7.7 | hf48f09d_4 19.8 MB
setuptools-47.1.1 | py37_0 515 KB
------------------------------------------------------------
Total: 24.4 MB
The following NEW packages will be INSTALLED:
ca-certificates pkgs/main/osx-64::ca-certificates-2020.1.1-0
certifi pkgs/main/osx-64::certifi-2020.4.5.1-py37_0
libcxx pkgs/main/osx-64::libcxx-10.0.0-1
libedit pkgs/main/osx-64::libedit-3.1.20181209-hb402a30_0
libffi pkgs/main/osx-64::libffi-3.3-h0a44026_1
ncurses pkgs/main/osx-64::ncurses-6.2-h0a44026_1
openssl pkgs/main/osx-64::openssl-1.1.1g-h1de35cc_0
pip pkgs/main/osx-64::pip-20.0.2-py37_3
python pkgs/main/osx-64::python-3.7.7-hf48f09d_4
readline pkgs/main/osx-64::readline-8.0-h1de35cc_0
setuptools pkgs/main/osx-64::setuptools-47.1.1-py37_0
sqlite pkgs/main/osx-64::sqlite-3.31.1-h5c1f38d_1
tk pkgs/main/osx-64::tk-8.6.8-ha441bb4_0
wheel pkgs/main/osx-64::wheel-0.34.2-py37_0
xz pkgs/main/osx-64::xz-5.2.5-h1de35cc_0
zlib pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3
Proceed ([y]/n)? y
Downloading and Extracting Packages
certifi-2020.4.5.1 | 155 KB | ##################################### | 100%
setuptools-47.1.1 | 515 KB | ##################################### | 100%
pip-20.0.2 | 1.7 MB | ##################################### | 100%
openssl-1.1.1g | 2.2 MB | ##################################### | 100%
python-3.7.7 | 19.8 MB | ##################################### | 100%
Preparing transaction: done
Verifying transaction: | WARNING conda.core.path_actions:verify(963): Unable to create environments file. Path not writable.
environment location: /Users/dao/.conda/environments.txt
done
Executing transaction: | WARNING conda.core.envs_manager:register_env(52): Unable to register environment. Path not writable or missing.
environment location: /Applications/anaconda3/envs/ComputerVision
registry file: /Users/dao/.conda/environments.txt
done
#
# To activate this environment, use
#
# $ conda activate ComputerVision
#
# To deactivate an active environment, use
#
# $ conda deactivate
下载完相关的库之后,我们需要来激活它
$ conda activate ComputerVision
然后
$ conda install -c conda-forge opencv
然后我们进入python,import cv2,未报错就配置成功了!
~ dao$ python
Python 3.7.7 (default, May 6 2020, 04:59:01)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
此时在ComputerVision下我们安装的python3.7是配置好OpenCV3的,所以如果我们要在Pycharm中食实验的话,我们就要将python的解释器路径修改为ComputerVision里的python3.7
参考资料: