1、安装虚拟环境
我用的是mac下的python3.6,在命令窗口用:
~ pip3 install virtualenv
Collecting virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 650kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
即可自动安装,当然如果你没有pip模块,则需要先安装pip,网上有很多pip安装教程,例如http://www.cnblogs.com/zdz8207/p/python_learn_note_16.html
安装完虚拟环境,则要开始使用了,在你的项目文件的目录执行:
cd /Users/zhangmeiyuan/PycharmProjects/MyProject
➜ MyProject ls
TEST [test1.py](http://test1.py)
➜ MyProject virtualenv venv
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/zhangmeiyuan/PycharmProjects/MyProject/venv/bin/python3.6
Also creating executable in /Users/zhangmeiyuan/PycharmProjects/MyProject/venv/bin/python
Installing setuptools, pip, wheel...done.
python3在WIN和MAC上自带pyvenv,不用额外安装。
mac:操作如下即可
mkdir .pyvenv
cd .pyvenv
pyvenv flask_venv
2、激活虚拟环境
zhangmeiyuan-5:.pyvenv zhangmeiyuan$ source flask_venv/bin/activate
3、安装Opencv过程及遇到问题解决
接下来就可以在虚拟环境中安装包,不影响外面的环境
问题一、pip版本未升级导致无法进行安装
然后尝试用pip命令进行安装
pip3 install opencv3
也报错了,报错信息如下:
Collecting opencv3
Could not find a version that satisfies the requirement opencv3 (from versions: )
No matching distribution found for opencv3
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(flask_venv) zhangmeiyuan-5:.pyvenv zhangmeiyuan$ pip3 install --upgrade pip3
Collecting pip3
Could not find a version that satisfies the requirement pip3 (from versions: )
No matching distribution found for pip3
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
原来是pip需要升级到更新版本,然后更新升级解决。
pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 470kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.1
Uninstalling pip-9.0.1:
Successfully uninstalled pip-9.0.1
Successfully installed pip-10.0.1
问题二、通过pip命令无法匹配opencv3安装包版本
但是仍然不能用pip进行opencv的安装
依旧报错
.pyvenv zhangmeiyuan$ pip3 install opencv3
Collecting opencv3
Could not find a version that satisfies the requirement opencv3 (from versions: )
No matching distribution found for opencv3
问题三:brew 版本问题导致安装不成功
既然pip无法安装,则使用brew命令进行安装,直接输入命令
brew install opencv3 --with-python3
报错信息如下:
Error: Could not link:
/usr/local/share/man/man1/brew.1
Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/doc/homebrew
Please delete these paths and run `brew update`.
Error: Xcode alone is not sufficient on El Capitan.
Install the Command Line Tools:
xcode-select --install
重新安装brew,解决brew版本问题
怀疑是之前安装的brew有问题
用ruby重新安装如下:
.pyvenv zhangmeiyuan$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装成功
然后再重新执行
brew install opencv
如果没有报错,说明安装成功了。
4、关联python3和opencv
接着看,我们下面的任务就是正确地使python3和opencv关联起来。注意到我们上面使用homebrew安装了程序,homebrew安装的程序位置都在 /usr/local/Cellar/ 下面,所以我们需要找到的就是opencv的cv2*.so文件,,最终我的so文件位置为:
/usr/local/Cellar/opencv3/3.1.0_4/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so
下面就是和python3环境关联,还有和虚拟virtualenv开发环境关联。
我关联的是虚拟环境,只要转到python3 的site-packages文件夹下面和原来上面的so建立一个软链接就行。
cd /usr/local/lib/python3.5/site-packages
ln -s /usr/local/Cellar/opencv3/3.1.0_4/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so cv2.so
cd /Users/zhangmeiyuan/PycharmProjects/MyProject/venv/lib/python3.6/site-packages
ln -s /usr/local/Cellar/opencv/3.4.1_5/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so
激活virtualenv虚拟环境,输入python
然后输入import cv2
后报错如下:
>>> import cv2
RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
看报错信息意思是numpy安装的版本有问题,删除之前安装的numpy文件夹后,再次输入pip install -U numpy,重新下载numpy。
$ pip3 install -U numpy
Collecting numpy
Downloading https://files.pythonhosted.org/packages/8e/75/7a8b7e3c073562563473f2a61bd53e75d0a1f5e2047e576ee61d44113c22/numpy-1.14.3-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.7MB)
100% |████████████████████████████████| 4.7MB 1.1MB/s
superset 0.19.1 has requirement cryptography==1.9, but you'll have cryptography 2.1.4 which is incompatible.
superset 0.19.1 has requirement requests==2.17.3, but you'll have requests 2.6.0 which is incompatible.
Installing collected packages: numpy
Found existing installation: numpy 1.13.1
Uninstalling numpy-1.13.1:
Successfully uninstalled numpy-1.13.1
Successfully installed numpy-1.14.3
5、检查是否安装成功
然后重新查看opencv是否真正安装成功。
>>> import cv2
>>> cv2.__version__
'3.4.1'
>>>
到此为止,mac下的python3 的opencv安装成功。